基於whiptail和shell腳本的交互式界面


官方使用手冊 https://linux.die.net/man/1/whiptail

1. 使用背景

在linux環境下開發軟件時,有時需要輸入一連串的交互命令才能得到我們想要的數據或者結果,而每次我們都要重新輸入這些指令,會比較繁瑣。如果有一種直觀的交互式界面工具來讓用戶輸入關鍵信息,工具內部隱藏實現細節,是不是更方便、高效呢,本文描述一種在shell腳本中使用whiptail指令來編寫交互式界面工具的方法。

2. whiptail指令介紹

2.1 什麼是whiptail

linux man page 對whiltail的描述爲:
whiptail is a program that will let you present a variety of questions or display messages using dialog boxes from a shell script。(大意爲:whiptail是一個允許你使用shell腳本中的對話框呈現各種問題或顯示消息的程序)。

2.2 whiptail常用語法

  • 在linux終端下輸入whiptail,可以看到如下輸出:
Box options:
        --msgbox <text> <height> <width>
        --yesno  <text> <height> <width>
        --infobox <text> <height> <width>
        --inputbox <text> <height> <width> [init]
        --passwordbox <text> <height> <width> [init]
        --textbox <file> <height> <width>
        --menu <text> <height> <width> <listheight> [tag item] ...
        --checklist <text> <height> <width> <listheight> [tag item status]...
        --radiolist <text> <height> <width> <listheight> [tag item status]...
        --gauge <text> <height> <width> <percent>
Options: (depend on box-option)
        --clear                         clear screen on exit
        --defaultno                     default no button
        --default-item <string>         set default string
        --fb, --fullbuttons             use full buttons
        --nocancel                      no cancel button
        --yes-button <text>             set text of yes button
        --no-button <text>              set text of no button
        --ok-button <text>              set text of ok button
        --cancel-button <text>          set text of cancel button
        --noitem                        don't display items
        --notags                        don't display tags
        --separate-output               output one line at a time
        --output-fd <fd>                output to fd, not stdout
        --title <title>                 display title
        --backtitle <backtitle>         display backtitle
        --scrolltext                    force vertical scrollbars
        --topleft                       put window in top-left corner
        -h, --help                      print this message
        -v, --version                   print version information

3. whiptail使用實例

3.1 使用whiptail實現Box options

3.1.1 msgbox

  • 基本使用方法:--msgbox <text> <height> <width>
  • 實例語法:whiptail --title "welcome"--msgbox "歡迎來到wytaitai的博客!" 10 60
  • 效果圖展示:
    msgbox效果展示

3.1.2 yesno

  • 基本使用方法:--yesno <text> <height> <width>
  • 實例語法:whiptail --yesno "是否要退出當前界面?" 10 60
  • 效果展示:使用左/右導航鍵可以選中yes/no按鈕,鍵入回車鍵確認選擇
    yesno效果展示

3.1.3 inputbox

  • 基本使用方法:--inputbox <text> <height> <width> [init]
  • 實例語法:whiptail --inputbox "請在此框中進行搜索" 10 60 "whiptail usage"
  • 效果展示:init用來默認搜索關鍵字
    inputbox效果展示

3.1.4 passwordbox

  • 基本使用方法:--passwordbox <text> <height> <width> [init]
  • 實例語法:whiptail --passwordbox "請輸入您的密碼:" 10 60 "12345678"
  • 效果展示:使用方法和inputbox基本一致
    在這裏插入圖片描述

3.1.5 textbox

  • 基本使用方法:--textbox <file> <height> <width>
  • 實例語法:whiptail --textbox "whiptail_shell.sh" 30 60
  • 效果展示:
    textbox效果展示

3.1.6 menu

  • 基本使用方法:--menu <text> <height> <width> <listheight> [tag item] ...
  • 實例語法:whiptail --menu "請選擇一個文件:" 15 60 4 "1" "whiptail_test1" "2" "whiptail_test2"
  • 效果展示:
    menu效果展示

3.1.7 gauge

  • 基本使用方法:--gauge <text> <height> <width> <percent>
  • 實例語法:whiptail --gauge "當前升級進度" 10 60 70
  • 效果展示:
    gauge效果展示

3.2 使用whiptail options,depend on box-option

3.2.1 title

  • 基本使用方法:--title <title>
  • 實例語法:whiptail --title "whiptail 練習" --msgbox "歡迎來到我的博客" 10 60
  • 效果展示:
    增加title效果展示

3.2.2 scrolltext

  • 基本使用方法:--scrolltext
  • 實例語法:whiptail --scrolltext --textbox "whiptail_shell.sh" 30 60
  • 效果展示:右邊垂直進度條,可以按/進行翻頁閱讀
    scrolltext效果展示

3.3 配合shell腳本實現用戶交互

命題:提示用戶輸入密碼登錄,密碼正確時,顯示歡迎界面,密碼錯誤時,提示用戶重新輸入,並提示剩餘嘗試次數;登陸系統成功後,在當前目錄下查找指定的文件,並將符合條件的文件列舉出來供用戶選擇;用戶選擇相應的文件後,打開文件,並可以上下翻頁查看文件內容;

3.3.1 登陸界面

登陸界面

3.3.2 密碼校驗界面

密碼校驗失敗
密碼校驗成功

3.3.3 搜索界面

搜索輸入界面

3.3.4 搜索結果展示

搜索結果展示

3.3.5 文件閱覽界面

文件閱覽

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章