Linux: Use C to call a bash script

This is a discussion on Linux: Use C to call a bash script within the Linux Programming forums, part of the Platform Specific Boards category; How do i run a bash script from a c program? Or rather is it possible to have all the ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Linux: Use C to call a bash script

    How do i run a bash script from a c program? Or rather is it possible to have all the features from bash programming in c?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by harada
    Or rather is it possible to have all the features from bash programming in c?
    Well... I believe that BASh is written in C... so that should answer that.
    Sent from my iPadŽ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    What are you trying to get bash to do which you can't do in C ?

    From past experience, it's something like trying to find files, which is easily do-able in C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    man system(3)

  6. #6
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Quote Originally Posted by Binary
    man system(3)
    The usage of system() function is not recommended! The program will not be portable for different shells!

    Instead of system() one can use fork()-execv(). Better and more portable!

  7. #7
    .
    Join Date
    Nov 2003
    Posts
    307
    popen is usable in that you can read the output of the executed command from your C code.

    man popen

  8. #8
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    >instead of system() one can use fork()-execv(). Better and more portable!
    Do we have a sys call that does the "cut" or "grep" operations?
    Portability is a big issue when it comes to using it, but I have somehow gotten into the habit of using it very often..
    In the middle of difficulty, lies opportunity

  9. #9
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    You don't have these system calls but instead you have a regex library. In my BSD system i have regex related functions. Use "man 3 -k regex" to list regex related manual pages.

    You may not have those functions in Win32 environment. If you care about portability you can write your own "cut" or "grep" functions. They are not very difficult to implement (at least implementing cut and grep).

  10. #10
    .
    Join Date
    Nov 2003
    Posts
    307
    A word about regex. There are flavors - for example the difference between ksh & bash patterns, grep, egrep, and perl. The syntax of the regular expressions in those environments changes a lot. ... from least complex to most complex.

    All this means is that the regex code you wrote may or may not support a regular expression you already have that works great -- elsewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 04:15 AM
  5. Linux Linux why Linux??
    By afreedboy in forum Tech Board
    Replies: 146
    Last Post: 01-21-2004, 05:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21