I have a bash script where I wish to write into a file if one of the commands inside an IF statement ends with nonzero (so when it exits with an error). However with the following I get a syntax error with an unexpected "else" at the end. Am I using this error writing right?
if [[ $f != */.* ]]; then
echo "$f"
command-one || { echo 'Something went wrong with Command-one at file ' $f ' !' >> ../corrupted.txt } || error=1
command-two || { echo 'Something went wrong with Command-two at file ' $f ' !' >> ../corrupted.txt } || error=1
command-three || { echo 'Something went wrong with Command-three at file ' $f ' !' >> ../corrupted.txt } || error=1
if [ error == 0 ]
then
echo "====================================================" >> ../ok.txt
echo "All went well with: " $f >> ../ok.txt
fi
error=0
else
echo "This file is corrupted: " $f >> ../corrupted.txt
fi
The problem you are dealing with here is a classic example of SC1083 - This {/} is literal. Check expression (missing ;/\n?) or quote it.
}
is literal because it's not at the start of an expression. We fix it by adding a ;
before it.
so add a ;
just before }
to indicate command-termination and double-quote all your variables as,
command-one || { echo "Something went wrong with Command-one at file ${f} !" >> ../corrupted.txt; } || error=1
command-two || { echo "Something went wrong with Command-two at file ${f} !" >> ../corrupted.txt; } || error=1
command-three || { echo "Something went wrong with Command-three at file ${f} !" >> ../corrupted.txt; } || error=1
Another would be to fix the comparison operator to
if [ $error -eq 0 ];