2017-03-08 50 views
0

所以我試圖運行一個腳本,將捕捉命令的輸出:擊:一定量的時間運行命令,並獲取其輸出

echo 'password' | sudo -S strace -p14750 -s9999 -e write 

約5秒鐘,然後存儲輸出變成一個變量。 我該怎麼做?

完整的腳本:

appium_pid_output=$(echo 'password' | sudo -S strace -p$appium_device_pid -s9999 -e write) 
    echo 'captured output of node '$appium_pid_output 

    if [[ $appium_pid_output == *POST* ]]; then 
     echo "device [ " + $device + " ] is currently in use" 
     return 1 
    fi 
+0

會很高興有一些反饋意見。我的答案有問題嗎? – Socowi

回答

0

使用timeout命令。

var="$(timeout 5 whateverProducesTheOutput)" 

缺點是超時不適用於複雜的命令。最簡單的方法是編寫一個函數並使用超時執行函數(請參閱this answer)。

foo() { 
    whateverProducesTheOutput 
} 
export -f foo 
var="$(timeout 5 bash -c foo)" 
相關問題