sha1sum
November 28th, 2013, 08:17 PM
Here is the problem I have:
Say we have a the file instruments.txt
guitar
piano
violin
cello
trumpet
ukelele
Now we do the following
#!/bin/bash
cat instruments.txt | while read; do
if [[ $REPLY =~ trumpet ]]; then
echo "trumpet is not a stringed instrument!"
exit 1
else
echo $REPLY
fi
done
echo "All are stringed instruments" # last line of the program
I expected that upon reading the line "trumpet", the program would exit. So the output should look like this:
guitar
piano
violin
cello
trumpet is not a stringed instrument!
However, the output I get is the following
guitar
piano
violin
cello
trumpet is not a stringed instrument!
All are stringed instruments
So what seems to happen is that the exit command doesn't exit the program, but only the loop. After that, the rest of the program is still executed.
How can I make the exit command exit the entire program?
Say we have a the file instruments.txt
guitar
piano
violin
cello
trumpet
ukelele
Now we do the following
#!/bin/bash
cat instruments.txt | while read; do
if [[ $REPLY =~ trumpet ]]; then
echo "trumpet is not a stringed instrument!"
exit 1
else
echo $REPLY
fi
done
echo "All are stringed instruments" # last line of the program
I expected that upon reading the line "trumpet", the program would exit. So the output should look like this:
guitar
piano
violin
cello
trumpet is not a stringed instrument!
However, the output I get is the following
guitar
piano
violin
cello
trumpet is not a stringed instrument!
All are stringed instruments
So what seems to happen is that the exit command doesn't exit the program, but only the loop. After that, the rest of the program is still executed.
How can I make the exit command exit the entire program?