09月01, 2020
收藏本站

shell中的菜单whiptail

先展示下效果,习惯了命令行操作,突然看到有这么一个交互式操作命令,有点眼前一亮。
menu.gif

主要代码:

#!/bin/bash
OPTION=$(whiptail --title "选择菜单 - Abul" --menu "Choose your option" 15 40 4 \
"1" "ALL" \
"2" "A->B" \
"3" "B->C" \
"4" "C-D"  3>&1 1>&2 2>&3)
status=$?
if [ $status = 0 ]; then
    echo "Your chosen option:" $OPTION
else
    echo "You chose Cancel."
fi

取到选择的值,后续就可以做一系列的操作,命令也比较简单,将菜单项替换成自己的即可。
当然whiptail还有很多交互式功能,比如:

  • 消息对话框 msgbox
    whiptail --title "Hello" --msgbox "Are you OK?" 15 30 image.png
  • yesno对话框
    whiptail --title "Hello" --yesno "Are you OK?" 10 30 image.png
  • 输入框inputbox
    whiptail --title "Hello" --inputbox "What is Your Name?" 10 30 image.png
  • 密码框passwordbox
    whiptail --title "Hello" --passwordbox "What is Your Password?" 10 30 image.png
  • 单选框radiolist
    whiptail --title "Hello" --radiolist "Choice One." 15 30 4 \
    "A" "Abul" OFF \
    "B" "Blue" OFF \
    "C" "Camille" OFF \
    "D" "Dabao" OFF
    image.png
  • 多选框checklist
    whiptail --title "Hello" --checklist "Choice One." 10 30 4 \
    "A" "Abul" OFF \
    "B" "Blue" OFF \
    "C" "Camille" OFF \
    "D" "Dabao" OFF
    image.png
  • 进度条gauge
    #!/bin/bash
    {
      for ((i = 0 ; i <= 100 ; i+=20)); do
          sleep 1
          echo $i
      done
    } | whiptail --gauge "Hold on,loading..." 6 60 0
    image.png

Comments