Calling a shell script from within C++ program

Discussion in 'C++' started by Juggler, Feb 24, 2005.

  1. Juggler

    Juggler Guest

    Hi,
    I have a shell script which takes three arguments, how can I call this
    script from within a C++ program.
    I am new to C++ programming and not sure how to accomplish this.
    I tried using system() call but could make it work, must be doing
    something wrong.
    Appreciate any help in this regard.

    Thanks,
    Juggler
    Juggler, Feb 24, 2005
    #1
    1. Advertising

  2. Juggler wrote:
    > I have a shell script which takes three arguments, how can I call this
    > script from within a C++ program.
    > I am new to C++ programming and not sure how to accomplish this.
    > I tried using system() call but could make it work, must be doing
    > something wrong.


    You have to consult your compiler/OS programming manual as to how to
    invoke shell scripts using the 'system' function. You probably have to
    specify what executable is to process your script, like

    system("/bin/sh scriptname");

    however, this is off-topic here, since C++ does not define how external
    commands are executed via 'system' function.

    V
    Victor Bazarov, Feb 24, 2005
    #2
    1. Advertising

  3. "Juggler" <> wrote in message
    news:...
    > I have a shell script which takes three arguments, how can I call this
    > script from within a C++ program.
    > I am new to C++ programming and not sure how to accomplish this.
    > I tried using system() call but could make it work, must be doing
    > something wrong.


    Probably. Using the system() call is the only way to do it with
    the standard C++ library.

    > Appreciate any help in this regard.


    I would recommend asking on a platform-specific forum, as several
    aspects of how system() works, and the other techniques you could
    use to run the shell script, will depend on your operating system.


    hth -Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Ivan Vecerina, Feb 24, 2005
    #3
  4. Juggler

    pradeepreddy3

    Joined:
    Nov 19, 2009
    Messages:
    1
    hae jugglar

    i can't help with respect with C++ but below code can help you out ...............


    This code is related to passing 3 parameteres to shell script

    #include<stdio.h>
    int main ( int argc , char* argv[] )
    {
    int i;
    char str[30];
    for ( i=1; i <= 3 ; i++ )
    printf( "%s \n" ,argv);

    sprintf(str,"/root/printthree %s %s %s",argv[1],argv[2],argv[3]);
    system(str);
    }

    where argv[] are arguments passed when C code is executed ....

    and shell script and we as simple as below
    NOTE: you have to mention full path of the script ...........................

    #!/bin/bash
    echo $1 $2 $3


    compile the above c code and at runtime pass three arguments ..........
    pradeepreddy3, Nov 19, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Heimes
    Replies:
    0
    Views:
    598
    Christian Heimes
    Feb 27, 2008
  2. Gerardo Herzig
    Replies:
    1
    Views:
    1,075
    Philipp Pagel
    Feb 27, 2008
  3. jay
    Replies:
    7
    Views:
    236
    Chad Perrin
    May 3, 2007
  4. ad
    Replies:
    5
    Views:
    329
    Uri Guttman
    May 30, 2004
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    248
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page