创建交互式shell脚本对话框
生活随笔
收集整理的這篇文章主要介紹了
创建交互式shell脚本对话框
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
當你在終端環境下安裝新的軟件時,你可以經常看到信息對話框彈出,需要你的輸入,比如:RHEL/CentOS自帶的setup,對話框的類型有密碼箱、檢查表、菜單等等。他們可以引導你以一種直觀的方式輸入必要的信息,使用這樣的用戶友好的對話框的好處是顯而易見的。如下圖所示:
當你寫一個交互式shell腳本,你可以使用這樣的對話框來接受用戶的輸入。whiptail可以在shell腳本中創建基于終端的對話框,消息框的過程,類似于Zenity或xdialog GUI腳本代碼。whiptail預先安裝在所有的Linux發布版本中。
語法:
whiptail --title "<message box title>" --msgbox "<text to show>" <height> <width>實例:
#!/bin/bash whiptail --title "Test Message Box" --msgbox "Create a message box with whiptail. Choose Ok to continue." 10 60語法:
whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>實例:
#!/bin/bash if (whiptail --title "Test Yes/No Box" --yesno "Choose between Yes and No." 10 60) thenecho "You chose Yes. Exit status was $?." elseecho "You chose No. Exit status was $?." fi
或者,你可以是“--yes-button” ,"--no-button"選項。
語法:
?whiptail --title "<input box title>" --inputbox "<text to show>" <height> <width> <default-text>實例:
#!/bin/bash PET=$(whiptail --title "Test Free-form Input Box" --inputbox "What is your pet's name?" 10 60 Wigglebutt 3>&1 1>&2 2>&3)exitstatus=$? if [ $exitstatus = 0 ]; thenecho "Your pet name is:" $PET elseecho "You chose Cancel." fi語法:
whiptail --title "<password box title>" --passwordbox "<text to show>" <height> <width>實例:
#!/bin/bash PASSWORD=$(whiptail --title "Test Password Box" --passwordbox "Enter your password and choose Ok to continue." 10 60 3>&1 1>&2 2>&3)exitstatus=$? if [ $exitstatus = 0 ]; thenecho "Your password is:" $PASSWORD elseecho "You chose Cancel." fi語法:
?whiptail --title "<menu title>" --menu "<text to show>" <height> <width> <menu height> [ <tag> <item> ] . . .實例:
#!/bin/bash OPTION=$(whiptail --title "Test Menu Dialog" --menu "Choose your option" 15 60 4 \ "1" "Grilled Spicy Sausage" \ "2" "Grilled Halloumi Cheese" \ "3" "Charcoaled Chicken Wings" \ "4" "Fried Aubergine" 3>&1 1>&2 2>&3)exitstatus=$? if [ $exitstatus = 0 ]; thenecho "Your chosen option:" $OPTION elseecho "You chose Cancel." fi語法:
?whiptail --title "<radiolist title>" --radiolist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .實例:
#!/bin/bash DISTROS=$(whiptail --title "Test Checklist Dialog" --radiolist \ "What is the Linux distro of your choice?" 15 60 4 \ "debian" "Venerable Debian" ON \ "ubuntu" "Popular Ubuntu" OFF \ "centos" "Stable CentOS" OFF \ "mint" "Rising Star Mint" OFF 3>&1 1>&2 2>&3)exitstatus=$? if [ $exitstatus = 0 ]; thenecho "The chosen distro is:" $DISTROS elseecho "You chose Cancel." fi語法:
?whiptail --title "<checklist title>" --checklist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .實例:
#!/bin/bash DISTROS=$(whiptail --title "Test Checklist Dialog" --checklist \ "Choose preferred Linux distros" 15 60 4 \ "debian" "Venerable Debian" ON \ "ubuntu" "Popular Ubuntu" OFF \ "centos" "Stable CentOS" ON \ "mint" "Rising Star Mint" OFF 3>&1 1>&2 2>&3)exitstatus=$? if [ $exitstatus = 0 ]; thenecho "Your favorite distros are:" $DISTROS elseecho "You chose Cancel." fi語法:
whiptail --gauge "<test to show>" <height> <width> <inital percent>實例:
#!/bin/bash {for ((i = 0 ; i <= 100 ; i+=20)); dosleep 1echo $idone } | whiptail --gauge "Please wait while installing" 6 60 0本文轉載自:https://www.linuxprobe.com/create-interactive-shell-script.html
轉載于:https://www.cnblogs.com/linuxprobe/p/5371594.html
總結
以上是生活随笔為你收集整理的创建交互式shell脚本对话框的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 正则表达式的顺序优先级
- 下一篇: 两个单体内置对象_Global和Math