我正在制作一个postinstall脚本,但由于某些原因,我的菜单在命令时无效。
它应该回应我选择的任何编辑器(好吧,现在),但只有echos 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
答案 0 :(得分: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'之一。 如果arg1等于,则这些算术二元运算符返回true 等于,小于,小于或等于,大于或等于 分别等于或等于arg2。 Arg1和arg2可能是正的或 负整数。
答案 1 :(得分: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