I want to write a simple loop like this:
for f in my_file0 my_file1 my_file2; do
(( $(find . -name $f | wc -l > 0 ))
done
However, I want the loop to break the script if fails and print an error message, so I did this:
for f in my_file0 my_file1 my_file2; do
(( $(find . -name $f | wc -l > 0 )) || echo error && exit 1
done
which exits at the first iteration, obviously.
So, I moved on to this solution
for f in my_file0 my_file1 my_file2; do
(( $(find . -name $f | wc -l > 0 )) || (echo error && exit 1)
done
which, as I understand it, only exits the sub-shell :-(
So, now I write it like this:
for f in my_file0 my_file1 my_file2; do
(( $(find . -name $f | wc -l > 0 ))
if (( $? > 0 )); then echo error && exit 1; fi
done
but that sucks...
What can I do, beside calling a function that will run those two commands?
break
in place ofexit 1
? This way you'll get out of for loop but not subshell.| wc -l
. And generally speaking: afind | wc -l
in afor f in files*
loop is a good solution for exactly nothing.