2014-01-12 92 views
0

我目前正在研究一個腳本,它需要檢查程序是否在第一次啓動時安裝。如果程序不存在,腳本將繼續進行安裝該程序所需的任何操作。到目前爲止,我已經試過如下:如何防止在運行腳本時出現「type」stdout?

program_exist=$(type "someprogram" | grep "not found") 
if ! "$someprogram_exist"; then 
     do some stuff 
fi 

program_exist=$(type "someprogram" 2>&1 >/dev/null | grep "not found") 
if ! "$someprogram_exist"; then 
     do some stuff 
fi 

但每次我跑這個時候,我總是會見了以下消息:

./some_program.sh: line 8: ./some_program.sh: line 7: type: someprogram: not found: No such file or directory 

有沒有一種方法來檢查程序是否存在而不是每次都顯示./some_program.sh: line 8: ./some_program.sh: line 7: type: someprogram: not found: No such file or directory消息?

+2

看到http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists -from-a-bash-script –

+0

謝謝!在該頁 – lacrosse1991

回答

3

只要試試這個:

if ! type someprogram &>/dev/null; then 
    do_some_stuff 
fi 

或更短:

type someprogram &>/dev/null || do_some_stuff 
+0

上有一些很好的建議,謝謝!看起來像我有我的'如果'語句設置導致一些問題的方式,雖然它現在像一個魅力 – lacrosse1991

相關問題