我使用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''也失敗了? –