Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 29th, 2006, 3:52 PM   #1
smile_sunshine
Newbie
 
Join Date: Apr 2006
Posts: 3
Rep Power: 0 smile_sunshine is on a distinguished road
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
smile_sunshine is offline   Reply With Quote
Old Apr 29th, 2006, 4:57 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 29th, 2006, 5:22 PM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.

>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!".
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Apr 29th, 2006, 5:33 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
ROFL! My mental (if I may loosely define it as that) translation was 'shell/batch script' :o . My apologies to the OP.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 29th, 2006, 7:21 PM   #5
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,193
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Apr 30th, 2006, 11:25 AM   #6
smile_sunshine
Newbie
 
Join Date: Apr 2006
Posts: 3
Rep Power: 0 smile_sunshine is on a distinguished road
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 is offline   Reply With Quote
Old Apr 30th, 2006, 12:02 PM   #7
smile_sunshine
Newbie
 
Join Date: Apr 2006
Posts: 3
Rep Power: 0 smile_sunshine is on a distinguished road
actually system () works fine for what i'm trying to do with it, thanks very much
smile_sunshine is offline   Reply With Quote
Old Apr 30th, 2006, 1:59 PM   #8
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Apr 30th, 2006, 10:36 PM   #9
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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.
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:57 AM.

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