Shell script continue after failure

2019-08-09 09:43发布

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.

标签: bash shell
2条回答
2楼-- · 2019-08-09 10:00

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-09 10:11

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
查看更多
登录 后发表回答