2013-01-19 36 views
1

嘗試使用wget執行腳本以下載文件,或者如果wget在Linux中不存在,則執行curl。如何讓腳本檢查wget的存在?檢查是否存在wget/curl

+0

[相似的](http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script)問題可以幫助你。 –

回答

3
wget http://download/url/file 2>/dev/null || curl -O http://download/url/file 
8

Linux有which命令,該命令將檢查可執行文件的路徑中的存在:

pax> which ls ; echo $? 
/bin/ls 
0 

pax> which no_such_executable ; echo $? 
1 

正如你所看到的,它集返回代碼$?輕鬆地告訴我們,如果可執行文件被發現。

0

你也可以使用commandtypehash檢查wget的/捲曲的存在與否。這裏的另一個線程 - 「Check if a program exists from a Bash script」很好地回答了在bash腳本中使用什麼來檢查程序是否存在。

我會做到這一點 -

if [ ! -x /usr/bin/wget ] ; then 
    # some extra check if wget is not installed at the usual place                   
    command -v wget >/dev/null 2>&1 || { echo >&2 "Please install wget or set it in your path. Aborting."; exit 1; } 
fi 
0

首先要做的就是儘量安裝與你平常的包管理系統安裝wget ,.它應該告訴你,如果已經安裝;

yum -y wget 

否則只是啓動命令像下面

wget http://download/url/file 

如果您收到任何錯誤,那麼它的確定。

+0

不是如何安裝,如何測試它的存在! –