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 ...
- 10-24-2011 #1Just 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..
- 10-24-2011 #2
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.
- 10-24-2011 #3Just 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
- 10-24-2011 #4
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.
- 10-24-2011 #5Just 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..
- 10-24-2011 #6Just 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...
- 10-24-2011 #7
Just for reference, I meant to implement s/th like this
C Tutorial – Command Line Parameter Parsing | CodingUnit Programming TutorialsYou must always face the curtain with a bow.
- 10-24-2011 #8Just Joined!
- Join Date
- Oct 2011
- Posts
- 34
thanks and i will try my best to understand it...anyway thanks for information
- 10-24-2011 #9Just 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-24-2011 #10
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.


Reply With Quote