unix system command - Unix

This is a discussion on unix system command - Unix ; Hi folks , I want to use system command in my code to run below commands in system. 1)mkdir -p /alcatel/var/share/AFTR/BSC; 2) mount -F lofs /alcatel/var/share/AFTR/BSC /var/sftp/bscsftp/ BSC 3) echo "/alcatel/var/share/AFTR/BSC - /var/sftp/bscsftp/ BSC lofs - yes -" >> /etc/vfstab ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: unix system command

  1. unix system command

    Hi folks ,

    I want to use system command in my code to run below commands in
    system.

    1)mkdir -p /alcatel/var/share/AFTR/BSC;

    2) mount -F lofs /alcatel/var/share/AFTR/BSC /var/sftp/bscsftp/
    BSC

    3) echo "/alcatel/var/share/AFTR/BSC - /var/sftp/bscsftp/
    BSC lofs - yes -" >> /etc/vfstab


    How can i do this ...
    i performed like this
    long status1 =system( ("mkdir -p /var/sftp/bscsftp/BSC").c_str() ) ;

    error is :
    const char* is not a structure type.


    how to do this..can sombody help ???

    Also third command need some special synatax i guess...any idea..

    regards
    Aki

  2. Re: unix system command

    On Fri, 24 Oct 2008 04:15:09 -0700, aki wrote:

    > i performed like this
    > long status1 =system( ("mkdir -p /var/sftp/bscsftp/BSC").c_str() ) ;
    > error is :
    > const char* is not a structure type.


    You are trying to compile some other language (C++ perhaps) with the C
    compiler.

    system( "mkdir -p /var/sftp/bscsftp/BSC" );

    > 3) echo "/alcatel/var/share/AFTR/BSC - /var/sftp/bscsftp/
    > BSC lofs - yes -" >> /etc/vfstab
    >
    > Also third command need some special synatax i guess...any idea..


    system() should be able to handle redirection if your shell can, just
    backslash escape the quotes:

    system( "echo \"/alcatel/... \" >> /etc/vfstab" );

    In general if you are using shell scripting it is much easier and quicker
    to save the commands as a shell script, rather than compiling a program
    and using the system() library function to invoke the shell interpreter.

    HTH
    viza

  3. Re: unix system command

    On Oct 24, 4:15*am, aki wrote:

    > How can i do this ...
    > i performed like this
    > long status1 =system( ("mkdir -p /var/sftp/bscsftp/BSC").c_str() ) ;
    >
    > error is :
    > const char* is not a structure type.
    >
    > how to do this..can sombody help ???


    Why would you call 'c_str' on something that is already a C-style
    string?

    DS

  4. Re: unix system command

    On Oct 24, 12:15*pm, aki wrote:

    > I want to use system command in my code to run below commands
    > in system.


    > *1)mkdir -p /alcatel/var/share/AFTR/BSC;


    > * 2) *mount -F lofs /alcatel/var/share/AFTR/BSC * /var/sftp/bscsftp/
    > BSC


    > * 3) *echo "/alcatel/var/share/AFTR/BSC * * - * * * /var/sftp/bscsftp/
    > BSC * * *lofs * *- * * * yes * * -" >> /etc/vfstab


    > How can i do this ...
    > i performed like this
    > long status1 =system( ("mkdir -p /var/sftp/bscsftp/BSC").c_str() ) ;


    > error is :
    > const char* is not a structure type.


    > how to do this..can sombody help ???


    Well, you have a char const[], which converts implicitly to the
    char const* required by system. You could always write
    system( std::string( "mkdir -p /var/sftp/bscsftp/BSC").c_str() ) ;
    but quite frankly, I don't see any point of it. Use std::string
    and its member function c_str() when you have to build up the
    command string; just use string literals directly.

    > Also third command need some special synatax i guess...any
    > idea..


    Why? The system function normally invokes a shell with "-c"
    your_string, so all of the usual shell operators are there.
    I've executed pipelines in system, and I've used it to start
    processes in background, as well as redirecting input and
    output.

    --
    James Kanze (GABI Software) email:james.kanze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  5. Re: unix system command

    On Oct 26, 3:12 pm, James Kanze wrote:
    > On Oct 24, 12:15 pm, aki wrote:
    >
    > > I want to use system command in my code to run below commands
    > > in system.
    > > 1)mkdir -p /alcatel/var/share/AFTR/BSC;
    > > 2) mount -F lofs /alcatel/var/share/AFTR/BSC /var/sftp/bscsftp/
    > > BSC
    > > 3) echo "/alcatel/var/share/AFTR/BSC - /var/sftp/bscsftp/
    > > BSC lofs - yes -" >> /etc/vfstab
    > > How can i do this ...
    > > i performed like this
    > > long status1 =system( ("mkdir -p /var/sftp/bscsftp/BSC").c_str() ) ;
    > > error is :
    > > const char* is not a structure type.
    > > how to do this..can sombody help ???

    >
    > Well, you have a char const[], which converts implicitly to the
    > char const* required by system. You could always write
    > system( std::string( "mkdir -p /var/sftp/bscsftp/BSC").c_str() ) ;
    > but quite frankly, I don't see any point of it. Use std::string
    > and its member function c_str() when you have to build up the
    > command string; just use string literals directly.
    >
    > > Also third command need some special synatax i guess...any
    > > idea..

    >
    > Why? The system function normally invokes a shell with "-c"
    > your_string, so all of the usual shell operators are there.
    > I've executed pipelines in system, and I've used it to start
    > processes in background, as well as redirecting input and
    > output.
    >
    > --
    > James Kanze (GABI Software) email:james.ka...@gmail.com
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    thakns for the feedback , i understood your point..

    now i have one more question .

    i need to run all above comamnds as root user from my c++ code .


    how can i do it ///??


    Any comments on this



    regards
    Aki


  6. Re: unix system command

    On Oct 28, 3:08*am, aki wrote:

    > thakns for the feedback , i understood your point..
    >
    > now i have one more question .
    >
    > i need to run all above comamnds as root user from my c++ code .
    >
    > how can i do it ///??
    >
    > Any comments on this


    There is no one easy, right, secure way. The simplest way is simply to
    run your program as root in the first place.

    Another way is to split your program into two halves. One half runs as
    root and executes commands after making absolutely sure that they are
    safe to run. The other half issues commands to the first half, telling
    it what commands to issue as root.

    In all cases, it is critical that the code be hardened to resist
    potential attacks. Writing software that is set-uid root is not for
    the faint of heart. That should be your very last choice.

    DS

+ Reply to Thread