How to run a tcsh shell command and selectively ignore the status?

别等时光非礼了梦想. 提交于 2019-12-05 10:13:16

I don't know about tcsh, but with bash, you can use set -e to do this. When the -e flag is set, bash will exit immediately if any subcommand fails (see the manual for technical details). When not set, it will continue to execute. So, you can do something like this:

set +e
cp file/that/might/not/exist .  # Script will keep going, despite error
set -e
cp file/that/might/not/exist .  # Script will exit here
echo "This line is not reached"

Here we go: Spawn a new shell, use ';' to ignore the first status, and it returns all clear.

$SHELL -c 'cp file/that/might/not/exist . ; echo "good"'
mustsucceed || exit 1
mustbeignored || :

If you don't care if it fails, remove the -e from your shebang. @Adam's answer ought to have provided a hint to you if you had looked at the tcsh documentation.

Also, you can throw away the error message:

cp dont_care       . >& /dev/null
cp still_dont_care . >& /dev/null || echo "not there"
cp must_be_there   . >& /dev/null || exit 1 # oh noes!
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!