Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
hey, im interested in creating shell script to execute my c program... for example my c program add to value of variable a and b..for example i have a =2 ...
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34

    just curious....can a shell script execute c program?

    hey,
    im interested in creating shell script to execute my c program...
    for example my c program add to value of variable a and b..for example i have a =2 and b=3...so the result is 5....
    can i use shell script to execute this c program as well as pass the value of a and b to that c program.if it can be done,please help me as i not really sure on how to do it...thanks in advance..

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,102
    Yes it can, happens all the time.
    A rough example would be s/th like this (the actual call of the c programm depends on how you implemented argument parsing)
    Code:
    #!/usr/bin/env bash
    
    a=2
    b=3
    c=$(your_c_program $a $b)
    echo $c
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    thanks for the quick reply...i did try these code similar to yours for many times but it does not work...

    so could u take a look at my code maybe there is something wrong with it..
    this is my c program called try.c:
    #include <stdio.h>
    int main()
    {
    int a,b,c;
    c=a+b;
    printf ("c=%d\n",c);
    return 0;
    }

    And this is my shell script called test,sh:
    #!/bin/sh

    a=23
    b=42
    c= $(./try $a $b)
    echo $c

    then i compile the c program to make it executable using gcc -o try try.c.
    i used ./try since it is in the same directory as my shell program.. and i also compile my shell using chmod +x test.sh...
    then to test whether passing value from shell to c program work,i used ./test.sh....but the result is not as i wish....maybe i did something wrong
    here...so please correct me...
    This is the ouput after using command ./test.sh:
    ./test.sh: line 6: 140571679: command not found

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,102
    The "try" variables are not the same as the bash ones.
    You will need to implement parameter handling.
    You must always face the curtain with a bow.

  5. #5
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    sorry i couldn't get what u mean...what do u mean by implementing the parameter handling and "try" variables are not the same as the bash ones...do u mean that i have to change my variables' name in c program....
    sorry im such a noob..

  6. #6
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    yeah it's k mate...now i get what u said and it's working now...thanks a lot for all information.....i will ask again if i got confuse in something else hehe...everything solve now...

  7. #7
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,102
    Just for reference, I meant to implement s/th like this
    C Tutorial – Command Line Parameter Parsing | CodingUnit Programming Tutorials
    You must always face the curtain with a bow.

  8. #8
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    thanks and i will try my best to understand it...anyway thanks for information

  9. #9
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    hey...just quick want to have a quick info...have u tried this kind of compilation before(passing value from shell to c program)? is it working for u?for me it still have error..dont know why...

  10. #10
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,102
    Back in university: yes

    You probably want to read some C books and/or do tutorials and/or take courses.
    If you then have a specific question, you can open a thread in the Programming/Scripting section here.
    You must always face the curtain with a bow.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...