Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling a shell script from within C++ program

Reply
Thread Tools

Calling a shell script from within C++ program

 
 
Juggler
Guest
Posts: n/a
 
      02-24-2005
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-24-2005
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
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      02-24-2005
"Juggler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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




 
Reply With Quote
 
pradeepreddy3 pradeepreddy3 is offline
Junior Member
Join Date: Nov 2009
Posts: 1
 
      11-19-2009
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 ..........
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to pass shell variable to shell script from python Gerardo Herzig Python 1 02-27-2008 12:19 PM
Re: How to pass shell variable to shell script from python Christian Heimes Python 0 02-27-2008 10:53 AM
execute a shell script in a shell script moongeegee Perl Misc 2 12-04-2007 12:18 AM
shell script with arguments within perl script jay Ruby 7 05-03-2007 08:56 AM
Setting ENV varibles by calling a shell script within a perl program ad Perl Misc 5 05-30-2004 04:18 AM



Advertisments