# Linux TUI課程-Dialog - [x] 本班學習目標:透過學習 Linux 建立自己==做筆記及自學摸索的方式== ###### tags: `新尖兵課程` ## Linux ISO 載點 * [ubuntu-24.04.1-live-server-amd64.iso](https://free.nchc.org.tw/ubuntu-cd) * [CentOS-7-x86_64-Minimal-2009.iso](http://ftp.ksu.edu.tw/pub/CentOS/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso) * [Rocky-8.8-x86_64-minimal.iso](https://free.nchc.org.tw/rocky/8/isos/x86_64/Rocky-8.8-x86_64-minimal.iso) ## dialog安裝 在 Unix 世界中,系統管理者普遍會用 shell script 撰寫簡單的管理工具。 為了強化易用性,簡化 shell script 的撰寫工作,就出現了專門負責提供圖形式互動元件的小工具, 可供我們在 shell script 中調用。 讓它們處理複雜的使用者輸入工作,同時也提高了 shell script 的使用親和力。 一、文字終端機環境: dialog。 非 Ubuntu 預裝項目。 ~~二、GNOME桌面環境: zenity。 這是 GNOME Desktop 計劃下的一個小工具。為 Ubuntu 桌面版本預裝項目,Ubuntu 桌面版都會安裝此套件。~~ ```python= sudo apt install dialog #安裝dialog套件 dialog --version #確認安裝版本 >Version: 1.3-20190808 ``` --- ## dialog牛刀小試 ### dialog牛刀小試-第一個專案 ```python= # dialog --title <title> --msgbox <text> <height> <width> dialog --title "訊息盒子" --msgbox "這是我第一個Dialog專案,就是簡單顯示GUI畫面" 10 50 ```  ### dialog更多參數 ```python= # 日曆,選擇時間 dialog --calendar <text> <height> <width> <day> <month> <year> # 訊息框,顯示資訊 dialog --msgbox <text> <height> <width> # 資訊入框,輸入私密資訊 dialog --inputbox <text> <height> <width> [<init>] # 密碼輸入框,輸入私密資訊 dialog --passwordbox <text> <height> <width> [<init>] # 確認視窗,確認訊息正確與否 dialog --yesno <text> <height> <width> # 文檔視窗,顯示文檔內容 dialog --textbox <file> <height> <width> # 選單,選擇清單 dialog --menu <text> <height> <width> <menu height> <tag1> <item1>... # 表單,可以依照表格輸入 dialog --form <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>... # 選項清單,選項可以進行複選 dialog --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>... # 編輯視窗,編輯文檔 dialog --editbox <file> <height> <width> # 進度條,顯示進度資訊 dialog --gauge <text> <height> <width> [<percent>] # 單選選單,只能單選一個選項 dialog --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>... ``` ### dialog牛刀小試-選擇一個日期 ```python= dialog --calendar "選擇一個日期" 4 50 9 6 2023 ```  ### dialog牛刀小試-訊息視窗 (使用 \n 換行) ```python= dialog --title "訊息視窗" --msgbox "Hello Gandalf" 10 50 dialog --title "訊息視窗" --msgbox "Hello Gandalf\nIt's a messagebox.\nHave a nice day." 10 50 ```  ### dialog牛刀小試-輸入單一資訊的輸入框 ```python= dialog --title "資訊輸入框" --inputbox "你的名字:" 10 50 ```  ### dialog牛刀小試-輸入單一輸入密碼(不會顯示輸入) ```python= dialog --title "請輸入密碼" --passwordbox "你的密碼:" 10 50 ```  ### dialog牛刀小試-yesno ```python= dialog --title "是非題" --yesno "人生的樂趣在於不可預期,同意?" 10 50 ```  ### dialog牛刀小試-抓取文字檔內容 ```python= dialog --title "文字檔內容呈現" --textbox ~/theword.txt 10 50 ```  ### dialog牛刀小試-選單模式 ```python= dialog --title "選單模式" --menu "可上下選擇" 10 50 3 1 "我可以" 2 "我想要" 3 "我不要" ```  ### dialog牛刀小試-表單模式 ```python= dialog --title "表單模式" --form "表單範例" 10 50 3 "Username:" 1 1 "" 1 15 25 0 "Age:" 2 1 "" 2 15 25 0 "Department:" 3 1 "" 3 15 25 0 dialog --title "表單模式" --form "表單範例" 10 50 3 \ "Username:" 1 1 "" 1 15 25 0 \ "Age:" 2 1 "" 2 15 25 0 \ "Department:" 3 1 "" 3 15 25 0 ```  ### dialog牛刀小試-選單模式 ```python= dialog --title "表單模式" --checklist "表單範例(可複選)" 10 50 2 1 "今天天氣好" on 2 "今天心情好" off dialog --title "表單模式" --checklist "表單範例(可複選)" 10 50 2\ 1 "今天天氣好" on\ 2 "今天心情好" off ```  ### dialog牛刀小試-編輯視窗 ```python= dialog --title "表單模式" --editbox theword.txt 30 50 ```  ### dialog牛刀小試-進度條 ```python= dialog --title "進度條" --gauge 目前進度 10 30 15 ```  ### dialog牛刀小試-單選選單 ```python= dialog --title "生理性別" --radiolist "單選" 10 30 2 1 "男生" on 2 "女生" off ```  --- ## dialog小型專案 ### 將輸入文字轉成變數輸出(方式一) vim inputbox.sh ```python= dialog --inputbox 請輸入文字 8 40 2> dialog-output; text=`cat dialog-output`; echo You input "$text".; ```  ### 將輸入文字轉成變數輸出(方式二) vim inputbox.sh ```python= text=`dialog --stdout --inputbox "請輸入文字" 8 40`; echo You input "$text".; ```  ### 將輸入文字轉成變數使用msgbox輸出 vim inputbox2msgbox.sh ```python= text=`dialog --stdout --inputbox "請輸入文字" 8 40`; dialog --title "你輸入的文字" --msgbox "$text" 8 40; ```   ### 輸入 IP 或 Domain 進行 Ping vim pingip.sh ```python= pingip=`dialog --stdout --inputbox "請輸入IP或網域名稱" 8 40`; dialog --title "你輸入IP或網域" --msgbox "$pingip" 8 40; ping $pingip -c 1 > pingip.txt; dialog --title "PING結果" --textbox pingip.txt 10 100; ```    ### 取得使用者的輸入結果 (echo $?) dialog 主要由程式結束碼(exit code)返回使用者輸入結果。 按 Unix shell script 慣例,0 表示正常、肯定等正面意義。 非零值(通常是1)皆表示錯誤、否定等錯誤意義。 若輸入結果為字串形式,則預設上將寫入標準錯誤輸出端(stderr)。 一般皆將其重導入暫存性檔案,再自檔案中讀出使用者輸入的內容。 vim yesno.sh ```python= dialog --yesno "是否繼續" 5 40 if [ $? -eq 0 ]; then echo "確定" else echo "取消" fi ```  ### 使用radiolist and case判斷式進行 Ping vim pingtest.sh ```python= dialog --stdout --title "選擇PING測試" --radiolist "單選" 10 30 2 1 "ping 8.8.8.8" on 2 "ping ntc.im" off 2> pingtest.txt pingtest=$(cat pingtest.txt) case $pingtest in 1) ping 8.8.8.8 -c 1 ;; 2) ping ntc.im -c 1 ;; *) echo "?" ;; esac ```   ### form表單取值 ```python= #--form <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>... # <label1> 之後 為顯示名稱 選單號 輸入起始 "預設內容" 欄位起始 欄位高 欄位寬 可輸入字數(0為不限制) vim form.sh dialog --stdout --title "表單模式" --form "個人資訊輸入" 10 50 3 "Username:" 1 1 "" 1 15 25 0 "Age:" 2 1 "" 2 15 25 0 "Department:" 3 1 "" 3 15 25 0 > form.txt line1=$(cat form.txt | sed -n "1p") line2=$(cat form.txt | sed -n "2p") line3=$(cat form.txt | sed -n "3p") dialog --title "你的個人資訊" --msgbox "你的名字是$line1 \n你今年$line2歲 \n你的部門是$line3 " 10 50 ```   ### 專案執行 驗收時間:2025/2/25 PM 03:30 驗收班級:DV107 組隊方式:個人專案 主題選擇:不限制(必須用到五種以上dialog形式進行互動) 建議主題:LAMP安裝與反安裝、~~ELK安裝與反安裝~~、Mariadb管理...等 交付方式:[提交Github連結](https://forms.gle/CyYPoyKrBNpe3s517) 專案參考: [dialog專案示範](https://hackmd.io/@ahahu/SJuL8shw2) ### 常見問題(也不一定常見) 1. [線條亂碼]很多時候,利用Putty或SSH連線到Linux,並且使用基於文字的使用者介面時會出現顯示亂碼的情況,如下:  要解決這個問題有以下的方法。 通常,因為這類程序是基於 ncurses (new curses) API,所以只要在SHELL中輸入: ```f= export NCURSES_NO_UTF8_ACS=1 ``` 2. [中文亂碼]改為UTF-8就可以解決中文亂碼的問題了 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up