我使用Ruby来调用通过shell脚本:使用Ruby来调用一个shell脚本退出代码总是返回0
%x[ /root/script.sh -k -m -l -w -p]
exitCode = %x[echo $?]
if exitCode != 0
do something
else
do something else
end
我的问题是退出代码始终为0,甚至当我逼脚本失败。我如何正确获取脚本的退出代码?
编辑: 好的来到今天早上开始通过脚本挖,因为我无法得到任何东西的错误代码0以外的脚本看起来像这样...
failed() {
if [ "$1" -ne 0 ] ; then
echo "$2 failed. INSTALLATION FAILED! Exiting.";
exit 1;
fi
}
{
function 1
function 2..
function ..20
} | tee -a logFile.log
所以最后脚本运行的东西总是这个日志文件,这意味着我永远不会得到真正的退出代码。
相关:[从Ruby调用shell命令](https://stackoverflow.com/questions/2232/calling-shell-commands-from-ruby)。你可以这样做'exitCode = $ ?. exitstatus' –
我刚刚试过,我得到一个TypeError 没有将Integer隐式转换为String。 所以我试着做exitCode = $ ?. exitstatus.to_i 我仍然得到相同的错误,虽然 – InsertNameHere
有趣。 'ruby -e'%x [/root/script.sh -k -m -l -w -p];退出码= $?退出状态;打印exitCode''也失败了? –