2011-05-23 57 views
2

我正在爲bash中的程序編寫安裝腳本。安裝的第一部分檢測程序是否已安裝。這通過指定目錄中文件的存在來檢測。這部分工作正常。但是,如果舊版本存在,我希望用戶能夠升級到新版本。我認爲我的代碼是正確的,但它不起作用。/var/www/html/gvsms中有一個名爲.version的文件,它只是在文件中具有該程序的版本。我想將該數字與安裝程序的編號進行比較。不過,我不確定在if語句中如何。 If裏面可以有多個Ifs?這是我到目前爲止有:Bash驗證和條件語句

$installerver=1.1 
$installedver=${cat /var/www/html/gvsms/.version} 
echo "Installation beginning...." 
echo " " 
echo " " 
echo -n "Checking to see if GVoice SMS Notification System was previously installed..." 
if [ -a /var/www/html/gvsms/index.php ]; then 
    echo "Yes." 
    echo "" 
    echo "I have detected a previous installation of GVoice SMS Notification System." 
    if [ $installerver == $installedver ]; then 
     echo "The GVoice SMS Notification System is currently installed and is the same version as this installer. If you were trying to upgrade, please check the installer you downloaded and try again." 
     exit 
    fi 
    elif [ $installedver == "0.5" || $installedver == "1.0"]; then 
    while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do 
     echo "Would you like to upgrade? Yes or no." 
     read -r upgrade 
     echo "$upgrade upgrade?" 
     echo "Is this correct? (Yes or No)" 
     read yn 
    done 
    echo "Upgrade proceeding..." 
    fi 
echo "No." 
echo "I have not detected a previous installation of GVoice SMS Notification System." 
read -p "To proceed with installation at your own risk, press Enter. Otherwise Ctrl-C." 

眼下與程序出現了錯誤:

./test.x: line 1: =1.1: command not found 
./test.x: line 2: ${cat /var/www/html/gvsms/.version}: bad substitution 

程序應該工作的方式是,如果檢測到文件,安裝的版本是不(或小於)與安裝程序相同,並且用戶說是升級,則應運行升級命令。如果用戶說不要升級,請退出腳本。

如果程序已安裝並且與安裝程序版本相同,則顯示錯誤消息並退出。

如果程序尚未安裝,請跳過重新確認安裝並繼續正常安裝。

這可能嗎? 謝謝!

+0

我想你忘了把[ hashbang](http://en.wikipedia.org/wiki/Shebang_%28Unix%29)在你的腳本行。嘗試在第一行插入'!#/ bin/bash'。 – voithos 2011-05-23 22:21:15

+0

我確實把它放在那裏,但它超出了這個粘貼的問題區域之外。 – Ross 2011-05-23 22:31:56

回答

0

您的作業有語法錯誤。分配給變量時,不要放置美元符號$。而對於命令替換,請使用括號$(...)

installerver=1.1 
installedver=$(cat /var/www/html/gvsms/.version) 

大括號爲簡單變量替換,如在echo "You bought $qty ${item}s."

+0

好吧,我解決了這個問題,並在變量定義處刪除了$。但現在我得到這樣的: '貓:/var/www/html/gvsms/.version:沒有這樣的文件或目錄 安裝開始.... 檢查,看看是否GVoice短信通知系統以前安裝。 ..是。 我檢測到以前安裝的GVoice SMS通知系統。 ./test.x:第13行:[:1.1:一元運算符預計 編號 我還沒有檢測到以前安裝的GVoice SMS通知系統。 要繼續進行安裝需要您自擔風險,請按Enter鍵。否則按Ctrl-C。 ' – Ross 2011-05-23 22:27:48

+0

'/var/www/html/gvsms/.version:沒有這樣的文件或目錄'..你絕對相信該文件存在?看起來像一個形而上學的確定性,它是一個代碼錯誤或錯誤的文件。檢查文件名本身沒有空格或其他非顯示字符,或者您的代碼以某種方式添加空格或其他字符。對於文件名,使用'ls -l /var/www/html/gvsms/.version* | cat -vet'查找文件名中的空格等。祝你好運! – shellter 2011-05-23 22:37:53

+0

對不起。我的錯。它現在確實存在。新的輸出是: '安裝開始.... 檢查以查看GVoice SMS通知系統是否先前已安裝...是。 我檢測到以前安裝的GVoice SMS通知系統。 編號 我還沒有檢測到以前安裝的GVoice SMS通知系統。 要繼續進行安裝需要您自擔風險,請按Enter鍵。否則按Ctrl-C。 ' – Ross 2011-05-23 22:49:01

2
  1. 的家當行添加到腳本頂部:

    #!/bin/bash 
    
  2. 變量賦值,而不使用美元符號進行:從一個子shell命令使用

    installerver=1.1 
    
  3. 變量賦值括號中沒有大括號:

    installedver=$(cat /var/www/html/gvsms/.version) 
    
  4. 有一個壞fi你如果塊

  5. 你錯過了一個else
  6. 你也許應該測試,如果文件cat'ing之前存在。
  7. 你應該用雙引號所有的變量
  8. 您需要有條件的括號[ … ][[ … ]]

內部空間這將讓你的代碼運行:

#!/bin/bash 

installerver="1.1" 
echo "Installation beginning...." 
echo " " 
echo " " 
echo -n "Checking to see if GVoice SMS Notification System was previously installed..." 
if [ -e "/var/www/html/gvsms/index.php" -a -r "/var/www/html/gvsms/.version" ]; then 
    echo "Yes." 
    echo "" 
    echo "I have detected a previous installation of GVoice SMS Notification System." 
    if [ "$(cat /var/www/html/gvsms/.version)" == "$installedver" ]; then 
     echo "The GVoice SMS Notification System is currently installed and is the same version as this installer. If you were trying to upgrade, please check the installer you downloaded and try again." 
     exit 
    elif [ "$installedver" == "0.5" || "$installedver" == "1.0" ]; then 
     while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do 
      echo "Would you like to upgrade? Yes or no." 
      read -r upgrade 
      echo "$upgrade upgrade?" 
      echo "Is this correct? (Yes or No)" 
      read yn 
     done 
     echo "Upgrade proceeding..." 
    fi 
else 
    echo "No." 
    echo "I have not detected a previous installation of GVoice SMS Notification System." 
    read -p "To proceed with installation at your own risk, press Enter. Otherwise Ctrl-C." 
fi 
+0

看起來棒極了!謝謝! 但是,它仍然不起作用。 '安裝開始.... 檢查GVoice短信通知系統是否以前安裝...是的。 我檢測到以前安裝的GVoice SMS通知系統。 ./test.x:第15行:[:missing']' ./test.x:line 15::command not found ' Grr。代碼標記混淆了錯誤。應該丟失']' – Ross 2011-05-23 22:53:59

+0

@Ross:您在右括號前還缺少一個空格。 – Gilles 2011-05-24 10:11:33