Jef*_*eff 9 eval sh exit xcodebuild
我正在尝试在shell脚本中创建一个函数,该脚本接受命令并使用eval执行它,然后根据命令的成功进行一些后处理.不幸的是,代码的行为并不像我期望的那样.这就是我所拥有的:
#!/bin/sh
...
function run_cmd()
{
# $1 = build cmd
typeset cmd="$1"
typeset ret_code
eval $cmd
ret_code=$?
if [ $ret_code == 0 ]
then
# Process Success
else
# Process Failure
fi
}
run_cmd "xcodebuild -target \"blah\" -configuration Debug"
Run Code Online (Sandbox Code Playgroud)
当命令($cmd)成功时,它工作正常.当命令失败时(例如编译错误),脚本会在我处理失败之前自动退出.有没有办法可以阻止eval退出,还是有一种不同的方法可以让我实现我想要的行为?
Wil*_*ell 14
如果脚本中有set -e某个地方,脚本应该只退出,所以我认为是这种情况.编写将阻止set -e触发自动退出的函数的更简单方法是:
run_cmd() {
if eval "$@"; then
# Process Success
else
# Process Failure
fi
}
Run Code Online (Sandbox Code Playgroud)
注意,function在定义函数时不可移植,并且()还使用冗余if .
| 归档时间: |
|
| 查看次数: |
11012 次 |
| 最近记录: |