LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-26-2004, 09:11 AM   #1
nuwandee
LQ Newbie
 
Registered: Mar 2004
Posts: 5

Rep: Reputation: 0
Unhappy 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]);

the output looks like this.

1:abcde
:abcde@domain.lk
:192.168.1.249
4:2004-2-29 22:05:46
:abcde@yahoo.com
6:0
7:0
:test-xp-01
9:email.content
0:1
11:0
12:0
13:1
14:email.envelope

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.

1:abcde
2:
:192.168.1.249
4:2004-2-29 22:05:46
:abcde@yahoo.com
6:0
7:0
:test-xp-01
9:email.content
0:1
11:0
12:0
13:1
14:email.envelope

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
 
Old 03-26-2004, 02:14 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Can you show the line from your script that calls the program, including all of the args.
 
Old 03-29-2004, 12:14 AM   #3
nuwandee
LQ Newbie
 
Registered: Mar 2004
Posts: 5

Original Poster
Rep: Reputation: 0
Unhappy

replying itsme86,

Here is my calling
./single_main_vcs "$username" "$fullusername" "$ip" "$formated" "$mail_to" "$attach" "$htmlscr" "$mail_subj" "$confile" "$nrcpt" "$blen_stat" "$allowtype" "$denytype" "$envfile"

to my knowledge it seems ok.
 
Old 03-29-2004, 05:21 AM   #4
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Code:
[yuray@admin414 yuray]$ cat t.c
#include <stdio.h>
int main(int argc, char *argv[])
{
        int i;
        for(i=1;i<argc;i++)
                printf("%d: %s \n",i,argv[i]);
        return 0;
}
[yuray@admin414 yuray]$ ./a.out a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
1: a1
2: a2
3: a3
4: a4
5: a5
6: a6
7: a7
8: a8
9: a9
10: a10
Could you show full C code and how you make call with parameters ?
 
Old 03-29-2004, 06:47 AM   #5
nuwandee
LQ Newbie
 
Registered: Mar 2004
Posts: 5

Original Poster
Rep: Reputation: 0
#include <stdio.h>
#include <string.h>

//Main Program starts
//
main(int argc, char *argv[])
{
char cv_uname[20];
char cv_funame[50];
char cv_ipadd[15];
char cv_dtime[20];
char cv_to[50];
char cv_attach[3];
char cv_htmlscr[3];
char cv_subj[100];
char cv_filename[60];
char cv_nrcpt[10];
char cv_blen_stat[3];
char cv_allowtype[3];
char cv_denytype[3];
char cv_envfile[60];
//
cv_uname[0]='\0';
cv_funame[0]='\0';
cv_ipadd[0]='\0';
cv_dtime[0]='\0';
cv_to[0]='\0';
cv_attach[0]='\0';
cv_htmlscr[0]='\0';
cv_subj[0]='\0';
cv_filename[0]='\0';
cv_nrcpt[0]='\0';
cv_blen_stat[0]='\0';
cv_allowtype[0]='\0';
cv_denytype[0]='\0';
cv_envfile[0]='\0';

//
strcpy(cv_uname,argv[1]);
strcpy(cv_funame,argv[2]);
strcpy(cv_ipadd,argv[3]);
strcpy(cv_dtime,argv[4]);
strcpy(cv_to,argv[5]);
strcpy(cv_attach,argv[6]);
strcpy(cv_htmlscr,argv[7]);
strcpy(cv_subj,argv[8]);
strcpy(cv_filename,argv[9]);
strcpy(cv_nrcpt,argv[10]);
strcpy(cv_blen_stat,argv[11]);
strcpy(cv_allowtype,argv[12]);
strcpy(cv_denytype,argv[13]);
strcpy(cv_envfile,argv[14]);

printf("Start point of the main\n");

printf("1:%s \n",cv_uname);
printf("2:%s \n",cv_funame);
printf("3:%s \n",cv_ipadd);
printf("4:%s \n",cv_dtime);
printf("5:%s \n",cv_to);
printf("6:%s \n",cv_attach);
printf("7:%s \n",cv_htmlscr);
printf("8:%s \n",cv_subj);
printf("9:%s \n",cv_filename);
printf("10:%s \n",cv_nrcpt);
printf("11:%s \n",cv_blen_stat);
printf("12:%s \n",cv_allowtype);
printf("13:%s \n",cv_denytype);
printf("14:%s \n",cv_envfile);
}


********************
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
 
Old 03-30-2004, 01:12 AM   #6
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Use strncpy instead strcpy.
 
Old 03-30-2004, 04:05 PM   #7
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X Lion
Posts: 80

Rep: Reputation: 16
linein delimiters may be contributing...

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.

Hope that helps.

Last edited by skoona; 03-30-2004 at 04:14 PM.
 
Old 04-01-2004, 02:01 AM   #8
nuwandee
LQ Newbie
 
Registered: Mar 2004
Posts: 5

Original Poster
Rep: Reputation: 0
yuray, I did try with the strncpy. But still the problem exists.

jscott000, I gave a try with double-double-quotes but the results the same.

I just tried with 2 aguments. in that case, I get the following results.
With

printf("[1:%s] \n",argv[1]);
printf("[2:%s] \n",argv[2]);

strcpy(cv_uname,argv[1]);
strcpy(cv_funame,argv[2]);

printf("[1:%s] \n",cv_uname);
printf("[2:%s] \n",cv_funame);

The output is as follows
[1:abcde]
] :abcde@queen.lk
++++++++++++++++++++++++++++++++++
Start point of the main
[1:abcde]
] :abcde@queen.lk

Please help me, I am clue less with this.
 
Old 04-01-2004, 09:57 AM   #9
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X Lion
Posts: 80

Rep: Reputation: 16
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.

I will dig deeper when you respond.
 
Old 04-01-2004, 12:49 PM   #10
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X Lion
Posts: 80

Rep: Reputation: 16
Show us how your script assigns values

Using this script...
---- example_parms
#! /bin/bash2

A1='a1'
A2='a2 suzy'
A3="a3 suzy"
A4='a4,now,is:time:for:all,good.'
A5='a5'
A6='a6'
A7='a7'
A8='a8'
A9='a9'
AA='aa'
AB='ab'
AC='ac'
AD='ad'
AE='ae'
AF='af'

echo "Un-Quoted"
./example $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9 $AA $AB $AC $AD $AE $AF

echo " Quoted"

./example ""$A1"" "$A2" "$A3" "$A4" "$A5" "$A6" "$A7" "$A8" "$A9" "$AA" "$AB" "$AC" "$AD" "$AE" "$AF"
---------

These results were produced...

----
$ ./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'

Last edited by skoona; 04-01-2004 at 12:57 PM.
 
Old 04-01-2004, 03:50 PM   #11
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X Lion
Posts: 80

Rep: Reputation: 16
In Summary...

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.
 
Old 04-06-2004, 06:10 AM   #12
nuwandee
LQ Newbie
 
Registered: Mar 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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.

But with C program it output foolowing

emailrelay.8859.244451.2.content
ConFILE: emailrelay.8859.244451.2.content
emailrelay.8859.244451.2.envelope
EnvFILE:emailrelay.8859.244451.2.envelope
2004 Apr 6 15:41:08
2004-4-6 15:41:08
Subject1
192.168.128.222
nuwan@king.informatics.lk
nuwan
1
Attachment 0
HTML 1
Allowed 0
Denyed 1
BLength 0
Recipient nuwan@vcs.lk
+++++++++++++++++++++++
[1:nuwan]
[2:]
] :192.168.128.222
[4:2004-4-6 15:41:08]
] :nuwan@vcs.lk
[6:0]
[7:1]
] :Subject1
[9:emailrelay.8859.244451.2.content]
] 0:1
[11:0]
[12:0]
[13:1]
[14:emailrelay.8859.244451.2.envelope]
++++++++++++++++++++++++++++++++++
Start point of the main
[1:nuwan]
[2:]
] :192.168.128.222
[4:2004-4-6 15:41:08]
] :nuwan@vcs.lk
[6:0]
[7:1]
] :Subject1
[9:emailrelay.8859.244451.2.content]
] 0:1
[11:0]
[12:0]
[13:1]
[14:emailrelay.8859.244451.2.envelope]


The First section is the echo outputs. Other two are argv[], and variable outputs.
 
Old 04-06-2004, 10:41 AM   #13
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X Lion
Posts: 80

Rep: Reputation: 16
Get double-quotes into the data

nuwandee,

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.

----------------------------------------------------------------------------------------------------
echo "Un-Quoted"
./example $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9 $AA $AB $AC $AD $AE $AF

echo " Quoted"
./example ""$A1"" "$A2" "$A3" "$A4" "$A5" "$A6" "$A7" "$A8" "$A9" "$AA" "$AB" "$AC" "$AD" "$AE" "$AF"
----------------------------------------------------------------------------------------------------

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.

Last edited by skoona; 04-07-2004 at 01:37 AM.
 
Old 04-10-2004, 01:12 AM   #14
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X Lion
Posts: 80

Rep: Reputation: 16
Talking 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.

FROM: char cv_funame[50];

TO: char cv_funame[128];


Last edited by skoona; 04-11-2004 at 06:14 PM.
 
Old 04-10-2004, 08:31 PM   #15
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X Lion
Posts: 80

Rep: Reputation: 16
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.

-- code --
#include <stdio.h>
#include <string.h>

struct IN_PARMS {
char cv_uname[128];
char cv_funame[128];
char cv_ipadd[128];
char cv_dtime[128];
char cv_to[128];
char cv_attach[128];
char cv_htmlscr[128];
char cv_subj[128];
char cv_filename[128];
char cv_nrcpt[128];
char cv_blen_stat[128];
char cv_allowtype[128];
char cv_denytype[128];
char cv_envfile[128];
} ;

//Main Program starts
//
int main(int argc, char *argv[])
{

struct IN_PARMS inparms;

memset (&inparms, 0, sizeof (struct IN_PARMS) );

strcpy(inparms.cv_uname,argv[1]);
strcpy(inparms.cv_funame,argv[2]);
strcpy(inparms.cv_ipadd,argv[3]);
strcpy(inparms.cv_dtime,argv[4]);
strcpy(inparms.cv_to,argv[5]);
strcpy(inparms.cv_attach,argv[6]);
strcpy(inparms.cv_htmlscr,argv[7]);
strcpy(inparms.cv_subj,argv[8]);
strcpy(inparms.cv_filename,argv[9]);
strcpy(inparms.cv_nrcpt,argv[10]);
strcpy(inparms.cv_blen_stat,argv[11]);
strcpy(inparms.cv_allowtype,argv[12]);
strcpy(inparms.cv_denytype,argv[13]);
strcpy(inparms.cv_envfile,argv[14]);

printf("\nStart point of the main #2\n");

printf("[1:%s] \n",inparms.cv_uname);
printf("[2:%s] \n",inparms.cv_funame);
printf("[3:%s] \n",inparms.cv_ipadd);
printf("[4:%s] \n",inparms.cv_dtime);
printf("[5:%s] \n",inparms.cv_to);
printf("[6:%s] \n",inparms.cv_attach);
printf("[7:%s] \n",inparms.cv_htmlscr);
printf("[8:%s] \n",inparms.cv_subj);
printf("[9:%s] \n",inparms.cv_filename);
printf("[10:%s] \n",inparms.cv_nrcpt);
printf("[11:%s] \n",inparms.cv_blen_stat);
printf("[12:%s] \n",inparms.cv_allowtype);
printf("[13:%s] \n",inparms.cv_denytype);
printf("[14:%s] \n",inparms.cv_envfile);

return 0;
}

-- end code --
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
call a c program in a shell script jagman Programming 4 04-05-2005 05:58 PM
Call a shell script from php? jharper101 Programming 2 02-15-2005 01:51 AM
How to call the arp linux shell command from within a perl program Bassam Programming 1 03-11-2004 03:00 AM
call shell or other program out of my_program lea Programming 3 10-09-2002 11:51 AM
how to put a shell call in a C/C++ program? Hano Programming 8 05-19-2002 03:26 PM


All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration