Urgent: Call a C program through Linux shell script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Urgent: Call a C program through Linux shell script
Hi all,
I have a bit strange issue. I have a shell script that fetch some information to shell variables and calls a C program with those shell variable.(14 variables passed).
However when this executes I miss one or two parameter values in the C program. (but not all). When I printf the values one by one argv[x] displays all the values. However even this If I use the following it prints with some missing Charaters.
printf("1:%s \n",argv[1]);
printf("2:%s \n",argv[2]);
printf("3:%s \n",argv[3]);
printf("4:%s \n",argv[4]);
printf("5:%s \n",argv[5]);
printf("6:%s \n",argv[6]);
printf("7:%s \n",argv[7]);
printf("8:%s \n",argv[8]);
printf("9:%s \n",argv[9]);
With missing printf characters. 2,3,5,8 and character 1 of 10 is missing.
After I assign the parameter to C variables with strcpy() and the printf returns the following.
Here I am using Bash shell on a Redhat 9.0. I tried with different version but the results same. However when I call the C program directly it outputs correctly. The problem exists only with Shell script and C integration.
I tried gcc versions gcc version 2.96 and gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7).
Please help me. I am stuck. Sorry for the long description. Thanks
********************
The C program works fine with the command prompt execution. But only issue is when it called throught the Shell script it outputs some missing character as shown in my initial query
Also consider how argv() parses linein values. space and commas are treated as delimiters- there more delimiters.... What's the typical value of your environment parms (i.e. "$fullusername") ? If they are not quoted "", the translated results could be wrong.
Did you consider using getenv() to retrieve the values. The environment keys could be stored in a configuration file - if you wanted to get real elaborate (or standard).
Your test data "a1 a2"etc would not typically cause a parse failure in this useage, no matter who/what called it. try wrapping double-double-quotes around any env-parm that could contain a space or comma - do this in your script and then test. example ""$fullusername"", should provide "multi part value", without double-double-quotes you would get "multi" as the sole value for that arg, arg+1 would have "part", and so on.
Please show two lines from your shell script. The lines that do the initial assignment to the environment parms. (ie. formated = , username = ).
You say the prog works fine from the command line, but fails when invoked from a shell script. The only diff is how the parms are initialized in the shell script - also the fact that the printf chars ( [1 ) are not printing is very curious.
----
$ ./example_parms
Un-Quoted
Start point of the main
1:a1
2:a2
3:suzy
4:a3
5:or:all,good.
6:a4,now,is:time:for:all,good.
7:a5
8:a6
9:a7
10:a8
11:a9
12:aa
13:ab
14:ac
Quoted
Start point of the main
1:a1
2:a2 suzy
3:a3 suzy
4:a4,now,is:time:for:all,good.
5:a5
6:a6
7:a7
8:a8
9:a9
10:aa
11:ab
12:ac
13:ad
14:ae
--------------
My gcc is gcc version 3.3.2 20031022 (Red Hat Linux 3.3.2-1)
I compiled your program like this: 'gcc -o example example.c'
I cannot seem to reproduce your results. Except maybe through how your script parms are initialized or passed to the C program in the un-quoted case. You will note that the input parms for the unquoted C invocation, really got clobered when the parm contains special chars and or multiple word values. This is expected and resolved by double-quoting the input parm as in the Quoted case.
Note: before calling the C program, try echo on each parm in your script file. Does it show the correct value? ie 'echo $username'
I think your C program is OK, and that your problem is in the script file. In particular, I think the script is assigning values to your parms incorrectly. I would like to see you spend time on validating your script first.
I compiled your program and tested it without errors of any type. I was able to induce simular errors by incorrectly initializing bash parms/vars.
Sorry to reply u late James. I did try as you did by just assigning a1, a2 values and the result ok.
I assign the valuses as follows.
fullusername="`awk '/MailRelay-From:/{print $2;exit}' \"${envelope}\"`"
As u said I echo all the shell varibles and they look ok. No missing characters.
Interesting results! It behaves as if the C program DID NOT get "" quoted stings. Again I still don't think its the C code. The command line parser built into the C compiler is attempting to parse your input causing this failure. We can tell the compiler/runtime to stop doing that by inserting and surrounding the value with double-quotes.
A validation that we got it right would be an 'echo $evalue' that produces "rvalue".
Try this.
fullusername=\""`awk '/MailRelay-From:/{print $2;exit}' \"${envelope}\"`"\" - or something like it, so that the echo output looks like this...
echo $fullusername
"nuwan@king.informatics.lk" -- notice the double quotes.
Those double-quotes will immediately solve your problem. I will look at a few bash operators to see if there is a better way to do it.
Using the above as the example, your output matches the "un-quoted" output from my earlier post. I assume that your script passes value to the C program using the "quoted" example! So we must go one step further by imbedding the double-quotes into the data - this will force the C-runtime to interpret the data correctly.
Let's Assume your Bash script is OK, and your C program is at Fault...
What could be the issue? Your parms are either out of order, or larger than you allotted for in your C program. Quick Test - Make all your C program strings 128 bytes in size.
A input parm larger than the string space will corrupt the string space and produce the kind of output you are showing.
Change the string sizes on all strings and give it a try.
Here is a new example, based on the string too small assumption!
If your strings are too small - try this example program. If it works then you need to determine which individual string is too short in your C program and correct it, or just waste a few bytes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.