Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   how do I call a bash script in C? (http://www.programmingforums.org/showthread.php?t=9592)

smile_sunshine Apr 29th, 2006 2:52 PM

how do I call a bash script in C?
 
Hi,
I'm new to learning C and just wondered if theres a way I can run a bash script from a C program (so that if a certain option is selected eg with switch case then a bash script runs)?

thanks
:)

DaWei Apr 29th, 2006 3:57 PM

You don't mention your OS/Platform. Since that's where these things get done, it would seem to be appropriate. Come back with that info. Meantime, read the forum's rules/FAQ and the "How to Post...." thread.

Narue Apr 29th, 2006 4:22 PM

>You don't mention your OS/Platform.
I think I'll go out on a limb and say that it's a POSIX compliant platform. :rolleyes:

>just wondered if theres a way I can run a bash script from a C program
Yes, there is. Generally, if you ask a question along the lines of "Can C do <so and so>", the answer is almost definitely "Yes!".

DaWei Apr 29th, 2006 4:33 PM

ROFL! My mental (if I may loosely define it as that) translation was 'shell/batch script' :o . My apologies to the OP.

lectricpharaoh Apr 29th, 2006 6:21 PM

Quote:

Originally Posted by smile_sunshine
Hi,
I'm new to learning C and just wondered if theres a way I can run a bash script from a C program (so that if a certain option is selected eg with switch case then a bash script runs)?

thanks
:)

I think what you want is system(). This loads an instance of the shell to handle the command you give it. However, use it with care; it's relatively easy for people to coerce it to do bad things, especially if you let them specify the command. If you hard-code the text of the command, though, it should be relatively safe.

smile_sunshine Apr 30th, 2006 10:25 AM

Hey,
OS is vector linux.
sorry I dont think I explained the question very well, I want to open a bash script (.sh file) I've already written within a C program so that partway through the program it also runs the bash script.



Narue - the question was more HOW can c do so and so! :) :)

thanks:)

smile_sunshine Apr 30th, 2006 11:02 AM

actually system () works fine for what i'm trying to do with it, thanks very much :)

Narue Apr 30th, 2006 12:59 PM

>Narue - the question was more HOW can c do so and so!
I know, but you'll find that programmers tend to be smartasses. If you unintentionally ask a yes or no question, you're damned sure to get a yes or no answer from at least one person. ;)

jim mcnamara Apr 30th, 2006 9:36 PM

man popen
man system
man exec

All of these man pages explain one way to do what you ask. Use popen if you want to get output - like stderr.


All times are GMT -5. The time now is 1:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC