Calling a shell script from within C++ program

J

Juggler

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
 
V

Victor Bazarov

Juggler said:
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
 
I

Ivan Vecerina

Juggler said:
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
 
Joined
Nov 19, 2009
Messages
1
Reaction score
0
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 ..........
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top