如何运行 tcsh shell 命令并有选择地忽略状态?
我有一个 tcsh shell 脚本,大多数时候我想以非零状态错误停止,但在某些情况下我想忽略它。例如:
#!/bin/tcsh -vxef
cp file/that/might/not/exist . #Want to ignore this status
cp file/that/might/not/exist . ; echo "this doesn't work"
cp file/that/must/exist . #Want to stop if this status is nonzero
I've got a tcsh shell script that I would like to stop with an error on nonzero status most of the time, but in some cases I want to ignore it. For example:
#!/bin/tcsh -vxef
cp file/that/might/not/exist . #Want to ignore this status
cp file/that/might/not/exist . ; echo "this doesn't work"
cp file/that/must/exist . #Want to stop if this status is nonzero
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道 tcsh,但是对于 bash,您可以使用
set -e
来执行此操作。当设置-e
标志时,如果任何子命令失败,bash 将立即退出(有关技术细节,请参阅手册)。不设置时,会继续执行。所以,你可以这样做: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:我们开始吧:生成一个新的 shell,使用 ';'忽略第一个状态,它返回全部清除。
Here we go: Spawn a new shell, use ';' to ignore the first status, and it returns all clear.
如果您不在乎它是否失败,请从 shebang 中删除
-e
。如果您查看了 tcsh,@Adam 的答案应该为您提供提示文档。另外,您可以丢弃错误消息:
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: