« Bug in NSDateFormatter. Not looking forward to ObjC 2.0... | Main | New Source of Income »

Test your shell scripting prowess

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)

dwt:

Uhm

This? "test.sh: line 9: [: -ne: unary operator expected"

cu Martin

Nat!:

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.

Nicola:

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.

Nat!:

Excellent. We have a winner!

About

This page contains a single entry from the blog posted on June 29, 2007 5:51 PM.

The previous post in this blog was Bug in NSDateFormatter. Not looking forward to ObjC 2.0....

The next post in this blog is New Source of Income.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.34