In a bash script, I have to check for multiple files.
I know an awkward way to do this, which means that my main program should be inside this ugly nested structure:
if [ -f $FILE1 ] then if [ -f $FILE2 ] then echo OK
The next version does not work:
([ -f $FILE1 ] && [ -f $FILE2 ]) || ( echo "NOT FOUND"; exit 1 ) echo OK
He is typing
NOT FOUND OK
Is there an elegant way to do it right?
UPDATE: See accepted answer. Also, in terms of elegance, I like Jonathan Leffler's answer :
arg0=$(basename $0 .sh) error() { echo "$arg0: $@ " 1>&2 exit 1 } [ -f $FILE2 ] || error "$FILE2 not found" [ -f $FILE1 ] || error "$FILE1 not found"
bash shell error-handling
Frank
source share