BASH: read every line in the files and use the line as parameters as another program
I am a file name par.file
200 200 0 0 900 199 200 0 0 900 100 200 0 0 900 I want to use a bash to read it line by line and then use that line to call a program, a.out. a.out 200 200 0 0 900 a.out 199 200 0 0 900 a.out 100 200 0 0 900 and then save the results of each calling of a.out into three variable what can my bash should like? I have tried other example in the web, but still have problems thx |
Welcome to LinuxQuestions.
Well there are probably many other better ways to do this but here's my version. #!/bin/bash exec 3< data while read <&3 do echo "The number is $REPLY" a.out "$REPLY" done exec 3>&- Now what it does is to open the file data and reads it line by line, sends that to a.out and finally closes the file. NOTE: the default variable for read is REPLY. HTH. |
could add a line EOF at the end of his file, then do while !EOF to make it so as many lines as he had would not matter?
|
I think this will work:
cat par.file | while read a; do set `a.out $a` v1=$1 v2=$2 v3=$3 done |
This is helpful especially when the statements inside the loop redirect something to stdin fooling `read`.
Quote:
|
try this:
Code:
cat par.file | while read $line;do also if your file is called code.c make code will create code rather than a.out |
Quote:
The effect of double quotes in a.out "$REPLY" is to pass the entire line from file data as a single argument to a.out. If that is not what is required, remove the double quotes. Regards "and then save the results of each calling of a.out into three variable", where do they appear and what is their format? Are they three whitespace-separated words on stdout? Anything on stderr? Exit status? |
Quote:
|
not true catkin,
observe... Code:
billy@foghorn:~$ while read x;do set `date`; done A big failing in bash IMHO (I prefer korn) |
Quote:
Code:
c:~$ echo '1 |
I have tried this and it works, but as soon as I try to call a function from inside my reading loop it only every reads the first line.
The file it is reading in is formated like: touch /tmp/t1 touch /tmp/t2 touch /tmp/t3 Here is the code that I have Code:
run_cmd_file() { Code:
run_cmd_file() { |
All times are GMT -5. The time now is 04:39 PM. |