2015-01-05 27 views
1

有沒有辦法捕捉使用腳本-c運行的程序的返回值?獲得命令運行腳本的返回值-c

例如(在bash)

/bin/false; echo $? # outputs 1 

/usr/bin/script -c "/bin/false" /dev/null; echo $? 
# outputs 0 as script exited successfully. 

我需要從/斌/返回值虛假的,而不是從/ usr/bin中/腳本..這可能嗎?我正在使用腳本欺騙程序,認爲它運行在真正的tty中,即使它不是......謝謝!

+3

根據'man script',好像添加了'-e'選項可以返回子進程的退出碼。 – ymonad

+0

我必須要喝更多的咖啡......我必須閱讀手冊頁3次,不知何故錯過了......謝謝! – user1390471

+0

我在頁面搜索了單詞「return」,並在幾秒鐘內找到它。 – Jasen

回答

0

根據man script,使用-e選項將返回子進程的退出代碼。

-e, --return 
     Return the exit code of the child process. 
     Uses the same format as bash termination 
     on signal termination exit code is 128+n. 

下面是一些例子。

$ /usr/bin/script -e -c "/bin/false" /dev/null; echo $? 
    1 
    $ /usr/bin/script -e -c "/bin/true" /dev/null; echo $? 
    0 
    $ /usr/bin/script -e -c "exit 123" /dev/null; echo $? 
    123