2016-01-26 74 views
0

我正在製作一個postinstall腳本,但由於某種原因,我的菜單不適用於if命令。

它應該回應我選擇的編輯器(呃,現在),但只有回聲emacs。幫幫我?

(下面的代碼)

#!/bin/bash 
if [ $(whoami) != root ] 
then 
    echo "Sorry, this script must be run as root. Try sudo su, and then running it!" 
    exit 1 
fi 

which dialog > /dev/null 

if [ $? = 1 ] 
then 
    echo "Sorry, you need to install dialog first." 
    exit 1 
fi 

choice=$(dialog --no-cancel --backtitle "Jacks Post Install Script" --title "Welcome" --menu "" 20 40 35 1 "Install crap already!" 2 "Enable ssh" 3 "Reboot" 4 "Exit" 3>&1 1>&2 2>&3 3>&-) 

function insstp1 { 
    dialog --cancel-label "No thanks" --extra-button --extra-label "Vim" --ok-label "Emacs" --backtitle "Jacks Post Install Script" --title "Pick an editor!" --yesno "I bet you will pick emacs. Seriusly." 5 40 
    echo $? #emacs 0 vim 3 no 1 
    if [ $? == 0 ] 
    then 
    echo "Emacs" 
    fi 

    if [ $? == 3 ] 
    then 
     echo "Vim" 
    fi 

    if [ $? == 1 ] 
    then 
    echo "nope" 
    fi 
} 

case $choice in 
    1) insstp1 ;; 
    2) enablssh ;; 
    3) reboot ;; 
    4) clear; exit 0 ;; 
esac 
+5

'回聲$'會的工作,但隨後將設置'$'的'echo'命令的退出代碼?。建議:立即在'dialog'之後,將'$?'的值存儲到另一個變量中。 –

+0

請注意,您可以通過簡單地測試命令的退出狀態'if commad;然後...; fi'。 –

回答

2

語法錯誤==-eq當然的避免回波

dialog --cancel-label "No thanks" --extra-button --extra-label "Vim" --ok-label "Emacs" --backtitle "Jacks Post Install Script" --title "Pick an editor!" --yesno "I bet you will pick emacs. Seriusly." 5 40 
choice2=$? 

if [ $choice2 -eq 0 ]; then 
    echo "Emacs" 
fi 

docs

arg1 OP arg2

OP是 '-eq' 中的一個, '-ne', '-lt', '-le', '-gt'或'-ge'。 這些算術二元運算符分別返回true,如果arg1等於,而不是 分別等於,小於,小於或等於,大於或大於 分別等於或等於arg2。 Arg1和arg2可能爲正或負整數。

+0

是的,但'[$? == 0]'將具有與[[$? -eq 0]'在'bash'中。 – chepner

0

店面$?和使用案例建設。您正嘗試將第一個對話框的結果存儲在變量選項中,但它將包含stdout。這樣的選擇應該與$?做,以及:

function insstp1 { 
     dialog --cancel-label "No thanks" --extra-button --extra-label "Vim" --ok-label "Emacs" --backtitle "Jacks Post Install Script" --title "Pick an editor!" --yesno "I bet you will pick emacs. Seriusly." 5 40 
     editchoice=$? #emacs 0 vim 3 no 1 
     case ${editchoice} in 
      0) echo "Emacs" ;; 
      3) echo "Vim" ;; 
      1) echo "nope";; 
      *) echo "Let me pick for you ...";; 
     fi 
    } 

dialog --no-cancel --backtitle "Jacks Post Install Script" --title "Welcome" --menu "" 20 40 35 1 "Install crap already!" 2 "Enable ssh" 3 "Reboot" 4 "Exit" 3>&1 1>&2 2>&3 3>&- 
choice=$? 

case ${choice} in 
    1) insstp1 ;; 
    2) enablssh ;; 
    3) reboot ;; 
    4) clear; exit 0 ;; 
esac