Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running C prog from ksh script

Status
Not open for further replies.

grega

Programmer
Feb 2, 2000
932
GB
Not sure if this is right forum - but someone should know :)

I have a simple C prog (I'm not a proficient C programmer) which accepts any number of command line arguments and processes them. When I run this from the command line - cprog arg1 arg2 arg3 arg4 arg5 ... it works fine.

I have a flat file containing lines of arguments, so I want to run the c prog for each line in the file. I'm doing this in a ksh script using a for..do..done loop. So for each line in the file, the script "appears to be" issuing the correct command (using set -x to check) - problem is the c prog is only seeing 2 arguments (the c prog name itself and it seems to be seeing my arg list from the file as 1 long arg).

Any ideas why this is happening?

Greg.
 
Hi Greg,
fragment below works OK
while read LN
do
cprog $LN
done < YourFlatFile
Regards Boris
 
What happens if you just echo the words in the list ?
ie
for filen in `cat arglist`
> do
> echo $filen
> done
Is each one on a separate line ? If not, the line terminator is perhaps not suitable for *nix.
HTH Dickie Bird (:)-)))
 
Will try above solutions. All args on one line, so I have to set IFS before the loop. Echoing the line read in the loop appears as it should.

Greg.
 
Greg,

By any chance did you use &quot;$*&quot; (with the quotes) in your script? This expands to &quot;$1 $2 ...&quot;. Replace it with &quot;$@&quot;, which expands to &quot;$1&quot; &quot;$2&quot; ...

Also,if your system has an xargs command, the following is simpler and faster than &quot;for&quot; and &quot;while&quot; loops:

xargs -l1 cprog <YourFlatFile
 
Thanks for all your help. In the end, the C prog was so simple I was able to replicate as functions in nawk (hadn't used them much so had forgotten about that ability), which is neater and faster. nawk now does all the work :)

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top