Can you tell the output of this shell script without running it ?
#!/bin/sh
echo "VfL Bochum 1848" | while read x
do
echo "$x"
rc=$?
done
if [ $rc -ne 0 ]
then
echo "echo reported an error"
fi
Comments (4)
Uhm
This? "test.sh: line 9: [: -ne: unary operator expected"
cu Martin
Posted by dwt | July 2, 2007 1:31 AM
Posted on July 2, 2007 01:31
Actually to be pedantic it's
VfL Bochum 1848
test.sh: line 9: [: -ne: unary operator expected
As your anwer is byte perfect,I have the suspicion that you did cheat a little, so do you know why it is saying that ?
That problem had me stumped in a real life script for quite some time.
Posted by Nat! | July 2, 2007 11:17 AM
Posted on July 2, 2007 11:17
The '|' causes both sides to be executed in a subshell. So the 'while' loop is executed in a subshell. That means that the 'rc' variable set in the 'while' loop is set in a subshell, and is lost when you get out of it.
When you later check the value of 'rc', it's an empty variable (undefined in the parent shell) so you'll get an error.
Posted by Nicola | November 10, 2007 6:48 PM
Posted on November 10, 2007 18:48
Excellent. We have a winner!
Posted by Nat! | November 13, 2007 2:55 PM
Posted on November 13, 2007 14:55