Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Calling a shell script from within C++ program (http://www.velocityreviews.com/forums/t289105-calling-a-shell-script-from-within-c-program.html)

Juggler 02-24-2005 05:49 PM

Calling a shell script from within C++ program
 
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


Victor Bazarov 02-24-2005 06:14 PM

Re: Calling a shell script from within C++ program
 
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

Ivan Vecerina 02-24-2005 06:21 PM

Re: Calling a shell script from within C++ program
 
"Juggler" <dynamo3209@yahoo.com> wrote in message
news:a7c74c6c254970631d4bf9222269b6a0@localhost.ta lkaboutprogramming.com...
> 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





pradeepreddy3 11-19-2009 07:18 AM

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[i]);

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 ..........


All times are GMT. The time now is 04:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.