I have the following script
cat $1 | while read line
do
line=`echo $line | tr "[:lower:]" "[:upper:]"`
if [ "`echo $line | cut -f1 -d:`" = "foo" ] && \
[ "`echo $line | cut -f2 -d:`" = "bar" ]; then
echo 'exsist'
exit 1;
fi
done
everything works up to echo and then when the script hits exit it does not and keeps going. Any ideas.
Thanks
You don’t need that backslash – this is not the C shell.
The problem is that the while loop is in a sub-shell, which exits, but because it is run as a sub-shell, the main script continues.
In the context, the simplest fix is probably:
If you have to process multiple files (‘cat “$@”‘ instead of ‘cat $1’), then you have to work a
lot harderbit harder:This checks the exit status of the pipeline consisting of ‘cat’ and ‘while’, which is the exit status of the ‘while’ loop, which will be 1 in the example if ‘foo:bar’ is found at the start of a line.
Of course, there are other ways to detect that, such as:
This executes a lot less commands than the loop version. (If you need to allow for ‘^foo:bar$’ as well, use egrep instead of plain grep.)