Shell script continue after failure

2019-08-09 09:46发布

问题:

How do I write a shell script that continues execution even if a specific command failed, however I want to output as error later, I tried this:

#!/bin/bash
./node_modules/.bin/wdio wdio.conf.js --spec ./test/specs/login.test.js
rc=$?
echo "print here"
chown -R gitlab-runner /gpc_testes/
chown -R gitlab-runner /gpc_fontes/
exit $rc

However the script stops when the node modules command fails.

回答1:

You could use

command_that_would_fail || command_failed=1
# More code and even more
.
.
if [ ${command_failed:-0} -eq 1 ]
then
 echo "command_that_would_fail failed"
fi


回答2:

Suppose name of the script is test.sh.

While executing the scripting, execute it with below command

./test.sh 2>>error.log

Error due bad commands won't appear on terminal but will be stored in file error.log which can be referred afterwards.



标签: bash shell