我正在製作一個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
'回聲$'會的工作,但隨後將設置'$'的'echo'命令的退出代碼?。建議:立即在'dialog'之後,將'$?'的值存儲到另一個變量中。 –
請注意,您可以通過簡單地測試命令的退出狀態'if commad;然後...; fi'。 –