四宝之尊 發表於 2023-8-21 00:00:00

Linux dialog详解(图形化shell)

<p>最近使用dialog写图形自动化shell脚本,  功能很强大,功能不是非常多但是足够用。想写一篇linux下dialog的使用方法,虽然命令不多,但是写起来也需要下很大功夫,而且不一定写得更好,在网上发现一篇linux shell图形化脚本文件,于是转过来了.</p>
<p>liunx 下的dialog 工具是一个可以和shell脚本配合使用的文本界面下的创建对话框的工具。<br>
每个对话框提供的输出有两种形式:<br>
1.  将所有输出到stderr 输出,不显示到屏幕。<br>
2.  使用退出状态码,“OK”为0,“NO”为1,"ESC"为255<strong><br></strong></p>
<div></div>
<div>通用选项 common options:(这个选项来设置dialog box的背景,颜色和 标题等)<strong>[--title &lt;title&gt;]  </strong>指定将在对话框的上方显示的标题字符串<br><strong>[--colors] </strong><strong>  </strong> 解读嵌入式“\ Z”的对话框中的特殊文本序列,序列由下面的字符 0-7, b  B, u, U等,恢复正常的设置使用“\Zn”。<br><strong>[--no-shadow]</strong><strong>  </strong> 禁止阴影出现在每个对话框的底部<br><strong>[--shadow] </strong>  应该是出现阴影效果<strong>[--insecure]</strong><strong>  </strong> 输入部件的密码时,明文显示不安全,使用星号来代表每个字符<strong>[--no-cancel] </strong>  设置在输入框,菜单,和复选框中,不显示“cancel”项<br><strong>[--clear] </strong><strong> </strong> 完成清屏操作。在框体显示结束后,清除框体。这个参数只能单独使用,不能和别的参数联合使用。<br><strong>[--ok-label &lt;str&gt;] </strong><strong> </strong> 覆盖使用“OK”按钮的标签,换做其他字符。<br><strong>[--cancel-label &lt;str&gt;]</strong><strong>  </strong>功能同上<strong><br>
[--backtitle &lt;backtitle&gt;] </strong>指定的backtitle字符串显示在背景顶端。<br><strong>[--begin &lt;y&gt; &lt;x&gt;]  </strong> 指定对话框左上角在屏幕的上的做坐标<br><strong>[--timeout &lt;secs&gt;]  </strong> 超时(返回的错误代码),如果用户在指定的时间内没有给出相应动作,就按超时处理<br><strong>[--defaultno] </strong>  使的是默认值 yes/no,使用no<br><strong>[--sleep &lt;secs&gt;]</strong><br><strong>[--stderr] </strong> 以标准错误方式输出<br><strong>[--stdout]</strong><strong>  </strong>以标准方式输出<strong><br>
[--default-item &lt;str&gt;]  </strong>设置在一份清单,表格或菜单中的默认项目。通常在框中的第一项是默认</div>
<p><strong>窗体类型:</strong></p>
<div>常见的对话框控件选项有:</div>
<div>
<b>[ --calendar ]</b>     提供了一个日历,让你可以选择日期</div>
<div>
<b>[ --checklist ]</b>    允许你显示一个选项列表,每个选项都可以被单独的选择  (复选框)</div>
<div>
<b>[ --from ] </b>          允许您建立一个带标签的文本字段,并要求填写</div>
<div>
<b>[ --fselect ]</b>    提供一个路径,让你选择浏览的文件</div>
<div>
<b>[ --gauge ] </b>   显示一个表,呈现出完成的百分比,就是显示出进度。</div>
<div>
<b>[ --infobox</b><b> ] </b>    显示消息后,(没有等待响应)对话框立刻返回,但不清除屏幕  (信息框)</div>
<div>
<b>[ --inputbox</b><b> ]  </b> 让用户输入文本  (输入框  )</div>
<div>
<b>[ --inputmenu </b><b>]</b>    提供一个可供用户编辑的菜单  (可编辑的菜单框)</div>
<div>
<b>[ --menu</b><b> ] </b>      显示一个列表供用户选择   (菜单框)</div>
<div>
<b>[ --msgbox </b><b>] </b>  显示一条消息,并要求用户选择一个确定按钮  (消息框  )</div>
<div>
<b>[ --pause </b><b>]  </b>    显示一个表格用来显示一个指定的暂停期的状态</div>
<div>
<b>[ --passwordbox</b><b> ] </b>   显示一个输入框,它隐藏文本</div>
<div>
<b>[ --passwordfrom</b><b> ]</b>  显示一个来源于标签并且隐藏的文本字段</div>
<div>
<b>[ --radiolist </b><b>]</b>      提供一个菜单项目组,只有一个项目,可以选择  (单选框 )</div>
<div>
<b>[ --tailbox</b><b> ] </b>       在一个滚动窗口文件中使用tail命令来显示文本</div>
<div>
<b>[ --tailboxbg</b><b>]</b>     跟tailbox类似,但是在background模式下操作</div>
<div>
<b>[ --textbox </b><b>] </b>      在带有滚动条的文本框中显示文件的内容  (文本框)</div>
<div>
<b>[ --timebox </b><b>] </b>     提供一个窗口,选择小时,分钟,秒</div>
<p><b>[ --yesno </b><b>] </b>    提供一个带有yes和no按钮的简单信息框  (是/否框)</p>
<p>如果没有此包请先安装<br>
yum -y install dialog</p>
<p><strong>命令示例:<br></strong><b>1.</b><b>消息框</b> 格式:dialog  - -msgbox  text   height  width</p>
<div>例子:</div>
<div><pre class="brush:bash;toolbar:false">#dialog    - -title   TESTING   - -msgbox  “this   is  a test “ 10  20</pre><div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/dac5d7438a85fd0a15f403aad461444c.jpg" width="300" height="174"></div>
<div><b>2.yesno框</b></div>
<div>格式:dialog –yesno  text  height  width<br>
例子:</div>
<div><pre class="brush:bash;toolbar:false">#dialog --title "yes/no" --no-shadow --yesno \
"Delete the file /tmp/chensiyao.txt?" 10 30</pre></div>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/6dbb8ebf0382e20616c85ef639656aa6.jpg" width="300" height="186"></div>
</div>
<div><b>3.输入框</b></div>
<div>格式:dialog --inputbox text height width<br>
例子:</div>
<p></p><pre class="brush:bash;toolbar:false"># dialog --title "Input your name"  \
--inputbox "Please input your name:" 10 30  2&gt; /tmp/name.txt  
#(这里的2&gt;是将错误信息输出重定向到了/tmp/name.txt文件中)</pre><p></p>
<div>
<img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/852bdfe4ec18b12cbc1436f3a4238eee.jpg" width="300" height="218"><b>4.密码框<br></b>
<div>格式:</div>
<div>dialog  --passwordbox text height width </div>
<div>例子:</div>
<div><pre class="brush:bash;toolbar:false"># dialog --title "Password"  --passwordbox \
"Please give a password for the new user:" 10 35</pre></div>
<div>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/cdce2afc9bec4f489f9075052aca5d10.jpg" width="300" height="188"></div>
<p>这样我们的密码就暴露出来了,是不是很不安全,所以通常我们会加上一个安全选项</p>
<div>--insecure   将每个字符用*来显示出来</div>
<div><pre class="brush:bash;toolbar:false"># dialog  --title  "Password"  --insecure  \
--passwordbox  "Please  give  a  password  for the  new  user:"  10  30</pre><div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/34c5395daee80e612856cc260b671452.jpg" width="300" height="228"></div>
</div>
</div>
</div>
<p> </p>
<div><b>5.文本框</b></div>
<div>格式:dialog --textbox file height width</div>
<div>例子:</div>
<p></p><pre class="brush:bash;toolbar:false">#dialog --title "The fstab" --textbox /etc/fstab  17 40</pre><p></p>
<div>
<img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/0b495b35c012e50a7f40a1452659b79a.jpg" width="300" height="229"><div><b>6.菜单框</b></div>
<div>格式:dialog --menu text height width  menu-height tag1 item1 tag2 item2 …</div>
<div>例子:</div>
<p></p><pre class="brush:bash;toolbar:false">#dialog --title "Pick a choice" --menu "Choose one" 12 35 5 \
1 "say hello to everyone" 2 "thanks for your support" 3 "exit"</pre><p></p>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/305a058f71e54023e4c4b68b34f0fe30.jpg" width="300" height="218"></div>
<div>
<b>7.Fselect框(</b><b>文件选框)</b>
</div>
<div>格式:dialog --fselect filepath height width</div>
<div>例子:</div>
<p></p><pre class="brush:bash;toolbar:false">#dialog --title "Pick one file" --fselect /root/ 7 40</pre><p><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/5ec408e720e3d2e3f134281c9aff3892.jpg" width="300" height="258"></p>
<p><b>8.复选框<br></b>格式:dialog  --checklist "Test" height width  menu-height  tag1 item1 tag2 item2 …<br>
例子:<b><br></b></p><pre class="brush:bash;toolbar:false"># dialog --backtitle "Checklist" --checklist "Test" 20 50 10 \
Memory Memory_Size 1 Dsik Disk_Size 2&lt;b&gt;&lt;/b&gt;</pre><p><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/b06e7d45fe7bbcd93b9881ac6cf35d1a.jpg" width="300" height="190"></p>
<p><b>9</b><b>.显示日历</b><br>
格式:dialog --calendar "Date" height width day month year<br>
例子:<br>
#显示当前日期</p><pre class="brush:bash;toolbar:false"># dialog --title "Calendar" --calendar "Date" 5 50</pre><p></p>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/8d58627c7b79fe8763b17999cd7d6ffc.jpg" width="300" height="268"></div>
<p>#显示指定日期</p><pre class="brush:bash;toolbar:false"># dialog --title "Calendar" --calendar "Date" 5 50 1 2 2013</pre><p></p>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/b1e58510e34d5a2b13baca0fb8f5c5b4.jpg" width="300" height="260"></div>
</div>
<div><b>10.进度框架</b></div>
<div>格式:dialog --gauge text height width  [&lt;percent&gt;]</div>
<div>例子:<br>
#固定进度显示<br><pre class="brush:bash;toolbar:false">#dialog --title "installation pro" --gauge "installation" 10 30 10</pre><div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/2cfb39279967c04616c8f07c465249ec.jpg" width="300" height="234"></div>
<p>#实时动度进度</p><pre class="brush:bash;toolbar:false">#for i in {1..100} ;do echo $i;done | dialog --title \
"installation pro" --gauge "installation" 10 30</pre><p></p>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/c569217c97655b9dd5a50381ad32eeac.jpg" width="288" height="219"></div>
</div>
<div>#编辑到脚本中<br>
编辑一个gauge.sh 的脚本</div>
<div>内容如下:</div>
<p></p><pre class="brush:bash;toolbar:false">#!/bin/bash  
# vim gauge.sh
declare -i PERCENT=0
(
        for I in /etc/*;do
                if [ $PERCENT -le 100 ];then
                        cp -r $I /tmp/test 2&gt; /dev/null
                        echo "XXX"
                        echo "Copy the file $I ..."
                        echo "XXX"
                        echo $PERCENT  
                fi
        let PERCENT+=1
        sleep 0.1
        done
) | dialog --title "coping" --gauge "starting to copy files..." 6 50 0</pre><p></p>
<div></div>
<div>#bash  gauge.sh  (执行脚本的时候注意修改权限)</div>
<div></div>
<p> </p>
<p><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/49a3d36959c3b105fe4e5b28d4ca012b.jpg" width="300" height="83"></p>
<div>
<b>11.from</b><b>框架</b>(表单)</div>
<div>格式:dialog --form text height width formheight [ label y x item y x flen ilen ] ...</div>
<div>其中</div>
<div>flen 表示field length,定义了:选定字段中显示的长度</div>
<div>ilen 表示 input-length, 定义了:在外地输入的数据允许的长度</div>
<div>使用up/down(或ctrl/ N,ctrl/ P)在使用领域之间移动。使用tab键在窗口之间切换。</div>
<div>例子:</div>
<div># dialog --title "Add a user" --form "Please input the infomation of new user:" 12 40 4  \</div>
<div>  "Username:" 1  1 "" 1  15  15  0  \</div>
<div>  "Full name:" 2  1 "" 2  15  15  0  \</div>
<div>  "Home Dir:" 3  1 "" 3  15  15  0  \</div>
<div>  "Shell:"    4   1 "" 4  15  15  0</div>
<p> </p>
<p> </p>
<p> </p>
<div>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/201996b791ecc30803d6f9b4120a6050.jpg" width="300" height="197"></div>
</div>
<p><span><strong>综合应用示例:</strong></span></p><pre class="brush:bash;toolbar:false">#!/bin/bash
yesno() {
dialog --title "First screen" --backtitle "Test Program" --clear --yesno \
"Start this test program or not ? \nThis decesion have to make by you. " 16 51
# yes is 0, no is 1 , esc is 255
result=$?
if [ $result -eq 1 ] ; then
exit 1;
elif [ $result -eq 255 ]; then
exit 255;
fi
username
}
username() {
cat /dev/null &gt;/tmp/test.username
dialog --title "Second screen" --backtitle "Test Program" --clear --inputbox \
"Please input your username (default: hello) " 16 51 "hello" 2&gt;/tmp/test.username
result=$?
if [ $result -eq 1 ] ; then
yesno
elif [ $result -eq 255 ]; then
exit 255;
fi
password
}
password() {
cat /dev/null &gt;/tmp/test.password
dialog --insecure --title "Third screen" --backtitle "Test Program" --clear --passwordbox \
"Please input your password (default: 123456) " 16 51 "123456" 2&gt;/tmp/test.password
result=$?
if [ $result -eq 1 ] ; then
username
elif [ $result -eq 255 ]; then
exit 255;
fi
occupation
}
occupation() {
cat /dev/null &gt;/tmp/test.occupation
dialog --title "Forth screen" --backtitle "Test Program" --clear --menu \
"Please choose your occupation: (default: IT)" 16 51 3 \
IT "The worst occupation" \
CEO "The best occupation" \
Teacher "Not the best or worst" 2&gt;/tmp/test.occupation
result=$?
if [ $result -eq 1 ] ; then
password
elif [ $result -eq 255 ]; then
exit 255;
fi
finish
}
finish() {
dialog --title "Fifth screen" --backtitle "Test Program" --clear --msgbox \
"Congratulations! The test program has finished!\n Username: $(cat /tmp/test.username)\n Password: $(cat /tmp/test.password)\n Occupation: $(cat /tmp/test.occupation)" 16 51
result=$?
if [ $result -eq 1 ] ; then
occupation
elif [ $result -eq 255 ]; then
exit 255;
fi
}
yesno</pre><p> </p>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/76f4330ff35848fb36d0dd44a5198f75.jpg" width="300" height="198"></div>
<p> </p>
<div align="left"><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/e40bab8b4b6d7fc4abe0cc0feabf48ca.jpg" width="300" height="203"></div>
<p> </p>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/1fe6d7a789b29928350b966b6445894f.jpg" width="300" height="202"></div>
<p> </p>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/76d668974ae8064b851096100beb72ea.jpg" width="300" height="198"></div>
<p> </p>
<div><img title="Linux dialog详解(图形化shell)" class="attachment-medium" src="https://zhuji.jb51.net/uploads/img/20230517/4bc1f2783dad3af55da7d551e4d70e42.jpg" width="300" height="202"></div>
<div></div>
<div>本文转自:http://gdcsy.blog.163.com/blog/static/1273436092013016069255/</div>
<div>本站整理:http://www.ttlsa.com/html/3085.html</div>
<div></div>
頁: [1]
查看完整版本: Linux dialog详解(图形化shell)