Try   HackMD

2024/02/27 Linux TUI課程-Dialog

課程講義連結
dialog專案(示範)
論壇-作業繳交

觀察可視化的差異

top

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

htop

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

bpytop

  • 安裝 sudo apt install bpytop
  • 離開q
  • 可視化更完整
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

Dialog牛刀小試

msgbox訊息視窗

dialog --title <title> --msgbox <text> <height> <width>
dialog --title "Hi Mango" --msgbox "2024/02/27 Linux TUI課程,這是我第一個Dialog專案,就是簡單顯示GUI畫面,今天(27日)仍受強烈大陸冷氣團影響,天氣寒冷,氣溫12-16度,請 務必多添衣保暖;有局部短暫雨,出門請攜帶雨具備用。" 10 50

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

訊息視窗 - 使用 \n 換行

dialog --title "訊息視窗" --msgbox "Hello Mango\nIt's a messagebox.\nHave a nice day." 10 50

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

calendar日曆,選擇一個日期

--calendar     <text> <height> <width> <day> <month> <year>
dialog --calendar "請選擇一個日期:" 4 50 27 2 2024

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

inputbox 輸入單一資訊的輸入框

dialog --title "資訊輸入框" --inputbox "你的名字:" 10 50

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

passwordbox 輸入單一輸入密碼(不會顯示輸入)

dialog --title "請輸入密碼" --passwordbox "你的密碼:" 10 50

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

yesno 是非題

dialog --title "是非題" --yesno "人生的樂趣在於不可預期,同意?" 10 50

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

抓取文字檔.txt內容

  • 建立檔名theword.txt,並將欲顯示的內容寫入
# 文檔視窗,顯示文檔內容
--textbox      <file> <height> <width>
dialog --title "文字檔內容呈現" --textbox ~/theword.txt 10 50

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

dialog --title "選單模式" --menu "可上下選擇" 10 50 3 1 "我可以" 2 "我想要" 3 "我不要"

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

form 表單模式

# 表單,可以依照表格輸入
--form         <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
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

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

checklist 選單模式(可複選)

# 選項清單,選項可以進行複選
--checklist    <text> <height> <width> <list height> <tag1> <item1> <status1>...
dialog --title "表單模式" --checklist "表單範例(可複選)" 10 50 2 1 "今天天氣好" on 2 "今天心情好" off
dialog --title "表單模式" --checklist "表單範例(可複選)" 10 50 2 \
1 "今天天氣好" on \ 
2 "今天心情好" off

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

radiolist 單選選單

# 單選選單,只能單選一個選項
--radiolist    <text> <height> <width> <list height> <tag1> <item1> <status1>...
dialog --title "生理性別" --radiolist "單選" 10 30 2 1 "男生" on  2 "女生" off

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

dialog牛刀小試-編輯視窗

dialog --title "表單模式" --editbox theword.txt 30 50

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

dialog牛刀小試-進度條

# 進度條,顯示進度資訊
--gauge        <text> <height> <width> [<percent>]
dialog --title "進度條" --gauge 目前進度 10 30 15

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

將輸入文字轉成變數輸出

  • 建立並編輯inputbox.sh檔
    -vim inputbox.sh

方式一

dialog --inputbox 請輸入文字 8 40 2> dialog-output;
text=`cat dialog-output`;
echo You input "$text".;

方式二

text=`dialog --stdout --inputbox "請輸入文字" 8 40`;
echo You input "$text".;

將輸入文字轉成變數使用msgbox輸出

  • 建立並編輯inputbox2msgbox.sh檔
    • vim iinputbox2msgbox.sh
text=`dialog --stdout --inputbox "請輸入文字" 8 40`;
dialog --title "你輸入的文字" --msgbox "$text" 8 40;

輸入 IP 或 Domain 進行 Ping

  • 建立並編輯pingip.sh檔
    • vim pingip.sh
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 $?)

  • 建立並編輯yesno.sh檔
    • yesno.sh
dialog --yesno "是否繼續" 5 40
if [ $? -eq 0 ]; then
    echo "確定"
else
    echo "取消"
fi
  • dialog 主要由程式結束碼(exit code)返回使用者輸入結果。
  • [ $? -eq 0 ]按 Unix shell script 慣例,0 表示正常、肯定等正面意義。 非零值(通常是1)皆表示錯誤、否定等錯誤意義。
  • 若輸入結果為字串形式,則預設上將寫入標準錯誤輸出端(stderr)。 一般皆將其重導入暫存性檔案,再自檔案中讀出使用者輸入的內容。
  • fi shell script語法 -(倒者寫) 結束if的語法

使用radiolist and case判斷式進行 Ping

  • 建立並編輯pingtest.sh檔
    • pingtest.sh
--radiolist    <text> <height> <width> <list height> <tag1> <item1> <status1>...
dialog  --stdout --title "選擇PING測試" --radiolist "單選" 10 30 2 1 "ping 8.8.8.8" on  2 "ping ntc.im" off 1> 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表單取值

  • 建立並編輯vim form.sh檔
    • vim form.sh
#--form         <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
# <label1> 之後 為顯示名稱 選單號 輸入起始 "預設內容" 欄位起始 欄位高 欄位寬 可輸入字數(0為不限制)
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

專案 : 製作UI套件,配合Shellscript

驗收時間:2024/3/4 AM
驗收班級:DV106
組隊方式:可同時個人及團隊編組
主題選擇:不限制(必須用到五種以上dialog形式進行互動)
建議主題:LAMP安裝與反安裝、ELK安裝與反安裝、Mariadb管理…等
交付方式:hackmd (參考 dialog專案示範

image

image

image

image

image

image

image

image

image

image

image