溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux圖形化編程dialog

發(fā)布時(shí)間:2020-08-01 16:49:34 來(lái)源:網(wǎng)絡(luò) 閱讀:2199 作者:woonli 欄目:開(kāi)發(fā)技術(shù)

----------------------------本文地址:https://blog.51cto.com/woonli/2142606
----------------------------參考網(wǎng)址http://linuxcommand.org/lc3_adv_dialog.php

一、簡(jiǎn)介
dialog是Linux圖形化自動(dòng)腳本編程的工具,實(shí)現(xiàn)腳本中圖形化,它是一個(gè)相當(dāng)大而復(fù)雜的程序(它有近100個(gè)命令行選項(xiàng)),但與典型的圖形用戶界面相比,它是一個(gè)真正的輕量級(jí)。盡管如此,它仍然具有許多用戶界面技巧;
二、使用條件
dialog在多數(shù)Linux發(fā)行版上已經(jīng)默認(rèn)安裝如果沒(méi)有安裝,
rpm系列用以下的其中一個(gè):
a、 yum install dialog -y
b、 rpm -vih dialog
deb系統(tǒng)用以下中的一個(gè):
a、sudo apt-get install dialog
b、sudo dpkg -i dialog
三、dianlog幫助信息
dialog有很多命令選項(xiàng),記憶困難,但如果理解了它,加上幫助信息,你會(huì)馬上擁有她;dialog的幫助很簡(jiǎn)單,在終端中輸入dialog命令即可;幫助信息如下:

cdialog (ComeOn Dialog!) version 1.3-20160828
Copyright 2000-2015,2016 Thomas E. Dickey
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

* Display dialog boxes from shell scripts *

Usage: dialog <options> { --and-widget <options> }
where options are "common" options, followed by "box" options

Special options:
  [--create-rc "file"]
Common options:
  [--ascii-lines] [--aspect <ratio>] [--backtitle <backtitle>] [--beep]
  [--beep-after] [--begin <y> <x>] [--cancel-label <str>] [--clear]
  [--colors] [--column-separator <str>] [--cr-wrap] [--date-format <str>]
  [--default-button <str>] [--default-item <str>] [--defaultno]
  [--exit-label <str>] [--extra-button] [--extra-label <str>]
  [--help-button] [--help-label <str>] [--help-status] [--help-tags]
  [--hfile <str>] [--hline <str>] [--ignore] [--input-fd <fd>]
  [--insecure] [--item-help] [--keep-tite] [--keep-window] [--last-key]
  [--max-input <n>] [--no-cancel] [--no-collapse] [--no-cr-wrap]
  [--no-items] [--no-kill] [--no-label <str>] [--no-lines] [--no-mouse]
  [--no-nl-expand] [--no-ok] [--no-shadow] [--no-tags] [--nook]
  [--ok-label <str>] [--output-fd <fd>] [--output-separator <str>]
  [--print-maxsize] [--print-size] [--print-version] [--quoted]
  [--scrollbar] [--separate-output] [--separate-widget <str>] [--shadow]
  [--single-quoted] [--size-err] [--sleep <secs>] [--stderr] [--stdout]
  [--tab-correct] [--tab-len <n>] [--time-format <str>] [--timeout <secs>]
  [--title <title>] [--trace <file>] [--trim] [--version] [--visit-items]
  [--week-start <str>] [--yes-label <str>]
Box options:
  --buildlist    <text> <height> <width> <list-height> <tag1> <item1> <status1>...
  --calendar     <text> <height> <width> <day> <month> <year>
  --checklist    <text> <height> <width> <list height> <tag1> <item1> <status1>...
  --dselect      <directory> <height> <width>
  --editbox      <file> <height> <width>
  --form         <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
  --fselect      <filepath> <height> <width>
  --gauge        <text> <height> <width> [<percent>]
  --infobox      <text> <height> <width>
  --inputbox     <text> <height> <width> [<init>]
  --inputmenu    <text> <height> <width> <menu height> <tag1> <item1>...
  --menu         <text> <height> <width> <menu height> <tag1> <item1>...
  --mixedform    <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>...
  --mixedgauge   <text> <height> <width> <percent> <tag1> <item1>...
  --msgbox       <text> <height> <width>
  --passwordbox  <text> <height> <width> [<init>]
  --passwordform <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
  --pause        <text> <height> <width> <seconds>
  --prgbox       <text> <command> <height> <width>
  --programbox   <text> <height> <width>
  --progressbox  <text> <height> <width>
  --radiolist    <text> <height> <width> <list height> <tag1> <item1> <status1>...
  --rangebox     <text> <height> <width> <min-value> <max-value> <default-value>
  --tailbox      <file> <height> <width>
  --tailboxbg    <file> <height> <width>
  --textbox      <file> <height> <width>
  --timebox      <text> <height> <width> <hour> <minute> <second>
  --treeview     <text> <height> <width> <list-height> <tag1> <item1> <status1> <depth2>...
  --yesno        <text> <height> <width>

Auto-size with height and width = 0. Maximize with height and width = -1.
Global-auto-size if also menu_height/list_height = 0.

四、幫助信息解析

Usage: dialog <options> { --and-widget <options> }

dialog 命令后是common options,即通用選項(xiàng),是圖形化框的一些個(gè)性化配置信息,依據(jù)是否需要可以省略;而--and-widget 是各類(lèi)圖形化部件,后邊重點(diǎn)介紹,部件的options主要是針對(duì)部件的高、寬、內(nèi)容、位置等參數(shù)的設(shè)置;
如幫助信息中的

  --buildlist    <text> <height> <width> <list-height> <tag1> <item1> <status1>...

是為了構(gòu)建并排顯示兩個(gè)列表。左側(cè)的列表包含未選擇的項(xiàng)目,右側(cè)所選項(xiàng)目的列表。用戶可以將項(xiàng)目從一個(gè)列表移動(dòng)到另一個(gè)列表。
在--buildlist的options中,

<text> <height> <width> <list-height> <tag1> <item1> <status1>...

"<text>"是自定義框體名稱(chēng),<height> <width>是框體的高和寬,默認(rèn)值為0,默認(rèn)時(shí)自適應(yīng)大小,<list-height>是列表選擇框的高度,<tag1> 是列表選擇框的提示或者標(biāo)題等;<item1>是列表的選項(xiàng); <status1>“可能是選擇該條目的狀態(tài),這個(gè)暫時(shí)也不清楚”
具體的命令如下:

dialog --title "列表框" --buildlist "選擇" 0 0 15 "USER" "CPU" 0 "MEM" "MEM" 1

具體效果如下
Linux圖形化編程dialog
OK~,其他選項(xiàng)框也是類(lèi)似的幫助信息;下面介紹部門(mén)options選項(xiàng)

  1. common options
    [--no-shadow] 禁止陰影出現(xiàn)在每個(gè)對(duì)話框的底部
    [--shadow] 應(yīng)該是出現(xiàn)陰影效果[--insecure] 輸入部件的密碼時(shí),明文顯示不安全,使用星號(hào)來(lái)代表每個(gè)字符[--no-cancel] 設(shè)置在輸入框,菜單,和復(fù)選框中,不顯示“cancel”項(xiàng)
    [--clear] 完成清屏操作。在框體顯示結(jié)束后,清除框體。這個(gè)參數(shù)只能單獨(dú)使用,不能和別的參數(shù)聯(lián)合使用。
    [--ok-label <str>] 覆蓋使用“OK”按鈕的標(biāo)簽,換做其他字符。
    [--cancel-label <str>] 功能同上
    [--backtitle <backtitle>] 指定的backtitle字符串顯示在背景頂端。
    [--begin <y> <x>] 指定對(duì)話框左上角在屏幕的上的做坐標(biāo)
    [--timeout <secs>] 超時(shí)(返回的錯(cuò)誤代碼),如果用戶在指定的時(shí)間內(nèi)沒(méi)有給出相應(yīng)動(dòng)作,就按超時(shí)處理
    [--defaultno] 使的是默認(rèn)值 yes/no,使用no
    [--sleep <secs>]
    [--stderr] 以標(biāo)準(zhǔn)錯(cuò)誤方式輸出
    [--stdout] 以標(biāo)準(zhǔn)方式輸出
    [--default-item <str>] 設(shè)置在一份清單,表格或菜單中的默認(rèn)項(xiàng)目。通常在框中的第一項(xiàng)是默認(rèn)。
  2. Windows options
對(duì)話 選項(xiàng) 描述
構(gòu)建列表 --buildlist 并排顯示兩個(gè)列表。左側(cè)的列表包含未選擇的項(xiàng)目,右側(cè)所選項(xiàng)目的列表。用戶可以將項(xiàng)目從一個(gè)列表移動(dòng)到另一個(gè)列表。
日歷 --calendar 顯示日歷并允許用戶選擇日期。
清單 --checklist 顯示選項(xiàng)列表,并允許用戶選擇一個(gè)或多個(gè)項(xiàng)目。
目錄選擇 --dselect 顯示目錄選擇對(duì)話框。
編輯框 --editbox 顯示基本文本文件編輯器。
形成 --form 允許用戶在多個(gè)字段中輸入文本。
文件選擇 --fselect 文件選擇對(duì)話框。
測(cè)量 --gauge 顯示進(jìn)度指示器,顯示完成百分比。
信息框 --infobox 顯示消息(帶有可選的定時(shí)暫停)并終止。
輸入框 --inputbox 提示用戶輸入/編輯文本字段。
菜單框 --menubox 顯示選項(xiàng)列表。
消息框 --msgbox 顯示文本消息并等待用戶響應(yīng)。
密碼箱 --passwordbox 與輸入框類(lèi)似,但隱藏用戶的條目。
暫停 --pause 顯示文本消息和倒數(shù)計(jì)時(shí)器。當(dāng)計(jì)時(shí)器用完或用戶按下“確定”或“取消”按鈕時(shí),對(duì)話框?qū)⒔K止。
程序框 --programbox 顯示管道命令的輸出。命令完成后,對(duì)話框?qū)⒌却脩舭聪翺K按鈕。
進(jìn)度框 --progressbox 類(lèi)似于程序框,除了對(duì)話框在管道命令完成時(shí)終止,而不是等待用戶按OK。
電臺(tái)名單 --radiolist 顯示選項(xiàng)列表,并允許用戶選擇單個(gè)項(xiàng)目。之前選擇的任何項(xiàng)目都將被取消選中。
范圍框 --rangebox 允許用戶使用基于鍵盤(pán)的滑塊在指定范圍內(nèi)選擇數(shù)值。
尾箱 --tailbox 顯示具有實(shí)時(shí)更新的文本文件。像命令一樣工作tail -f。
文本框 --textbox 簡(jiǎn)單的文本文件查看器。支持許多相同的鍵盤(pán)命令less。
時(shí)間盒 --timebox 用于輸入時(shí)間的對(duì)話框。
樹(shù)視圖 --treeview 顯示樹(shù)形層次結(jié)構(gòu)中的項(xiàng)目列表。
是/否盒子 --yesno 顯示文字信息,讓用戶有機(jī)會(huì)回答“是”或“否”。

五、在shell腳本中使用
dialog圖形化主要是在運(yùn)維過(guò)程中圖形自動(dòng)化腳本人機(jī)交互時(shí)使用,圖形化的選擇或提示等,可操作行更強(qiáng)。
dialog在腳本中使用需要主要以下兩點(diǎn):
a、dialog在繪制會(huì)話框本身時(shí)使用標(biāo)準(zhǔn)輸出作為終端顯示,所以在會(huì)話框上的輸入(例如輸入到輸入框中的字符串)通常通過(guò)標(biāo)準(zhǔn)錯(cuò)誤返回,這就導(dǎo)致了dialog獲取數(shù)據(jù)的問(wèn)題,要解決該問(wèn)題,需要將錯(cuò)誤輸出轉(zhuǎn)換為標(biāo)準(zhǔn)輸出文件提示符;解決該問(wèn)題的方法有兩種:
1)、使用臨時(shí)存儲(chǔ)文件
2)使用文件描述符
b、dialog圖形化中的狀態(tài)碼,在使用dialog前先了解狀態(tài)碼;如“OK”為0,“NO”為1,"ESC"為255,還可以定義其他狀態(tài)碼,如幫助信息等等。

六、腳本實(shí)例
本腳本實(shí)例中為解決錯(cuò)誤輸出轉(zhuǎn)換問(wèn)題使用文件描述符,基本上多數(shù)dialog腳本中都使用文件描述符;格式為
exec 3>&1
dialog --widdget options 2>&1 1>&3
exec 3>&1;
該命令包括將描述符2(stderr)重定向?yàn)槊枋龇?的副本,最后,通過(guò)復(fù)制包含備份副本的描述符3,將描述符1恢復(fù)為其原始值??赡懿粫?huì)立即顯示的是為什么需要最后一次重定向。在子shell中,標(biāo)準(zhǔn)輸出(描述符1)不指向控制終端。相反,它指向一個(gè)將其內(nèi)容傳遞給變量的管道result。由于dialog需要標(biāo)準(zhǔn)輸出指向終端以便它可以顯示輸入框,我們必須將標(biāo)準(zhǔn)錯(cuò)誤重定向到標(biāo)準(zhǔn)輸出(以便輸出dialog結(jié)束于result 變量),然后將標(biāo)準(zhǔn)輸出重定向回控制終端。
腳本實(shí)例:

#!/bin/bash
#Dialog use case
#Author by woon

## Define the dialog exit status codes
#可以不定義退出碼。直接使用值也行。
: ${DIALOG_OK=0}
: ${DIALOG_CANCEL=1}
: ${DIALOG_HELP=2}
: ${DIALOG_EXTRA=3}
: ${DIALOG_ITEM_HELP=4}
: ${DIALOG_ESC=255}

#定義msgbox函數(shù)
display_box() {
dialog --title "$1" \
    --no-collapse \
    --msgbox "$res" 0 0
    }
while true;do
    #將標(biāo)準(zhǔn)輸出復(fù)制一份到文件描述符3,  
    exec 3>&1
    selection=$(dialog --title "系統(tǒng)監(jiān)測(cè)" \
        --clear \
        --ok-label "Submit" \
        --cancel-label "Exit" \
        --menu "選擇監(jiān)測(cè)項(xiàng):" 0 0 10 \
        "1" "當(dāng)前用戶占用資源情況" \
        "2" "當(dāng)前磁盤(pán)使用情況" \
        "3" "CPU相關(guān)信息" \
        "4" "MEM相關(guān)信息" \
        2>&1 1>&3   #將錯(cuò)誤輸出轉(zhuǎn)換為標(biāo)準(zhǔn)輸出
        )

    exit_status=$?
    exec 3>&- #關(guān)閉文件描述符3
    case $exit_status in 
        $DIALOG_CANCEL)
        clear
        echo "程序終止"
        exit
        ;;
    $DIALOG_ESC)
    clear 
    echo "程序終止"
    exit 1
    ;;
esac
case $selection in 
    0)
        clear
        echo "程序終止"
        ;;
    1)
        pcpu=$(ps -eo user,pcpu,pmem | awk -v user=$USER '$1==user{print $0}'|awk '{pcpu+=$2}END{print pcpu}')
        pmem=$(ps -eo user,pcpu,pmem | awk -v user=$USER '$1==user{print $0}'|awk '{pcpu+=$2}END{print pcpu}')
        res="當(dāng)前用戶占用CPU:"$pcpu"\n當(dāng)前用戶占用內(nèi)存:"$pmem
        display_box "當(dāng)前用戶占用資源情況"
        ;;
    2)
        res=$(df -h)
        display_box "當(dāng)前磁盤(pán)使用情況"
        ;;
    3)
        res=$(dmesg | grep CPU | awk -F "] " '{print $2}')
        display_box "CPU硬件信息"
        ;;
    4)
        res=$(head -10 /proc/meminfo )
        display_box "系統(tǒng)內(nèi)存信息"
        ;;
esac
done
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI