I have no idea how you arrived at this, but if this would work, shell programming woud be a bad idea.
First: forget about backticks. They are just there for backwards compatibility with a shell which is not in use any more since about 25 years. Just forget they exist.
Second: even if you use backticks, x=`some -command`
is a variable assignment. In fact it does: run the command some -command
and assign its output to variable x. So, what you did amounts to:
while read line ; do
command
done < x="blabla"
which is obviously not what you intended. You do it the same way i showed you with sed
: by pipelining it like this:
command |\
while read line ; do
other_command "$line"
done
The pipeline can have more than one part, so this would also be possible:
command1 |\
command2 |\
command3 |\
.... |\
while read line ; do
other_command "$line"
done
This runs command1
, then pours its output into command2
, the output of command2
into command3
and so forth, until the output of the last command is poured into the loop, which reads it line by line and processes it.
I am sure you can now construct your own script with ease, no?
By the way: if you want to process lines like you do stay generally away from for
-loops. While-loops are a lot more flexible when it comes to parsing input, even when word-splitting is involved.
Suppose you have a file (or process output, which is effectively the same) of this structure:
line1-word1 line1-word2 line1-word3
line2-word1 line2-word2 line2-word3
line3-word1 line3-word2 line3-word3
line4-word1 line4-word2 line4-word3
and you want to process the second word of every line ("line1-word2", "line2.word2", etc.) then this will do it:
InFile="/path/to/input"
secondword=""
while read junk secondword junk ; do
echo "this is $word"
done <"$InFile"
Try it out. You could do that with some gymnastics and a for-loop too, but this is IMHO a lot easier to write and a lot easier to read and understand.
I hope this helps.
bakunin