BASH: read every line in the files and use the line as parameters as another program
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.
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.
This is helpful especially when the statements inside the loop redirect something to stdin fooling `read`.
Quote:
Originally Posted by /bin/bash
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.
#!/bin/bash
exec 3< data
while read <&3
do echo "The number is $REPLY"
a.out "$REPLY"
done
exec 3>&-
That looks good.
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?
cat par.file |
while read a; do
set `a.out $a`
v1=$1
v2=$2
v3=$3
done
The variables v1 to v3 will not be set outside the loop because the "cat | while" pipeline results in bash running the while in a sub-shell. When the sub-shell terminates, variables set within it are lost.
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() {
client=$1
while read line
do
echo "+ $line"
chr=${line:0:1}
case $chr in
"#" ) # Currently we ignore commented lines
;;
* )
run_cmd $client $line
;;
esac
done < $2
}
when I run this, it will read in the first line of the file "touch /tmp/t1" but doesn't read in the rest. But if i do this code:
Code:
run_cmd_file() {
client=$1
while read line
do
echo "+ $line"
chr=${line:0:1}
case $chr in
"#" ) # Currently we ignore commented lines
;;
* )
echo "client[$client] line[$line]"
;;
esac
done < $2
}
it runs fine. I checked my other functions and none of them are calling exit or anything and when I run it with bash -x, bash shows me that the second time it hits the while loop (aka after handling first line and reading in second) it exits. Anyone have any idea while it would be acting this way?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.