我经常添加set -e
我的bash脚本.但是这次我必须调用一个命令,在成功时返回一些无意义的数字而不是0.如何告诉bash忽略此命令的返回值.
更改命令或更改其代码以符合标准不是一种选择.
nan*_*dhp 11
true
始终返回零退出代码.所以你可以做到
command-with-meaningless-return-value || true
Run Code Online (Sandbox Code Playgroud)
如果你需要使用$?
后面的值,你可以这样做:
# A test funtion to demonstrate different exit status values
$ exit_with(){ return $1; }
$ exit_with 0
$ echo $?
0
$ exit_with 4
$ echo $?
4
$ exit_with 4 || true
$ echo $?
0
# capture the exit status rather than clobber it
$ exit_with 4 || exit_status=$?
$ echo $?
0
$ echo $exit_status
4
Run Code Online (Sandbox Code Playgroud)
在这种情况下,使用$exit_status
代替$?
. 我还没有找到做类似?=$?
或declare ?=$?
归档时间: |
|
查看次数: |
318 次 |
最近记录: |