Trying to implement self syntax checking in bash script test.sh
to run syntax check before script will be executed.
error_exit()
{
echo -e "$1" 1>&2
exit 1
}
bash -n "$(basename $BASH_SOURCE)"
RESULT=$?
if [ $RESULT == 0 ]; then
echo "[OK] Test pass"
else
error_exit "Something is not right :( \n check the sytntax"
fi
starting-script
bash -n
, same as bash -x
does the job if there are some serous syntax errors
but minor errors like:
echo "somthing" && sleep5&&
./test.sh: line 14: sleep5: command not found
are ignored...
is there any way to process a full syntax check plus may be running scrip in some sort of "sandbox mode"/"simulation mode" to catch all possible errors, and return with error if any?
edited In cases when script has to copy some part of the script in tot the remote server, for instance script to install nagios on hundreds of slave(monitored servers) etc. Unfortunately text copying do not always goes smoothly, and errors can be caused by occurred errors while automatically copying script as a text, and it is important to check and end script, if any errors are detected.