shell脚本7---图形化shell

select

select 命令允许从单个命令行创建菜单,然后在提取输入并自动处理。格式如下:

select <variable> in <list>
do
    commands
done

list参数是构成菜单的空格分隔的文本选项列表。select命令会在列表中将每个选项作为一个编好号的选项显示,然后为选项显示一个特殊的由PS3环境变量定义的提示符。
举例

#!/bin/bash

PS3="Enter option:"
select option in "Display disk space" "Display logged users" "Display memory usage" "Exit program"
do
    case $option in
        "Exit program")
              break  ;;
        "Display disk space")
              <commands or functions>;;
         "Display logged users")
              <commands or functions>;;
        "Display memory usage")
              <commands or functions>;;
        *)
              clear
              echo "Sorry, wrong selection.";;
      esac
done

执行上面的脚本会有如下输出

1) Display disk space    3) Display memory usage
2) Display logged users  4) Exit program
Enter option:

在使用select命令时要注意,存储在变量中的是整个文本字符串而不是与之关联的数字。

dialog

dialog命令使用命令行参数来决定生成哪种窗口部件(widget)。部件是dialog包中窗口元素类型的术语,dialog支持的部件类型

部件 描述
calendar 提供选择日期的日历
checklist 显示多个选项(其中每个选项都能打开或者关闭)
form 构建一个表单(用标签和文本字段来填充)
fselect 提供一个文件选择窗口来浏览选择文件
gauge 显示完成的百分比进度条
infobox 显示一条消息,但不用等待回应
inputbox 提供一个输入文本用的文本表单
inputmenu 提供一个可编辑的表单
menu 显示可选择的一系列选项
msgbox 显示一条消息,并要求用户选择OK按钮
pause 显示一个进度条来显示特定暂定时间的状态
passwordbox 显示一个文本框来输入文本,但会隐藏输入的文本
passwordform 显示一个带标签和隐藏文本输入的表单
radiolist 提供一组菜单选项,但只能选择其中一个
tailbox 用tail命令在滚动窗口中显示文件的内容
tailboxbg 和tailbox一样,但是在后台模式中运行
timebox 提供一个选择小时、分钟和秒的窗口
yesno 提供一个简单的带yes和no按钮的窗口
msgbox
dialog --title 'Message' --msgbox 'Hello, world!' 5 20

创建一个message box,标题是Message,内容是“Hello, world!”。高是5,宽20,会有一个OK按钮在上面。
大多数的部件创建格式都是相同的:一个可选的title,对话框类型,显示的文本,对话框的高和宽,外加其他额外的参数。

yesno
dialog --title "Message"  --yesno "Are you having\ fun?" 6 25

这个命令运行后,在对话框上会看到两个按钮,分别写着"Yes"和"No"。如果选择yes那么返回给shell的状态码就是0,选择no返回的是1。

infobox
dialog --infobox "Please wait" 10 30 ; sleep 4

infobox只显示一段信息,而不用等着用户按OK按钮。

inputbox
dialog --inputbox "Enter your name:" 8 40 2>answer

inputbox允许用户输入一串字符,在用户按OK之后,字符被写道标准错误输出或者指定的文件描述符。

textbox
dialog --textbox /etc/profile 22 70

textbox是一个简单的文件查看器,参数是一个文件名。常用的方向键可以使用,或者vi的方向键。

menu

格式如下

dialog --menu <text> <height> <width>
<menu-height> [<tag><item>]

menu用来创建一个可以被选择的菜单。每一个菜单由tag和与之关联的item组成。用户可以使用光标来选定tag并按enter键。被选择的itag会被选到标准输出,举例如下:

dialog --menu "Choose one:" 10 30 3 1 red 2 green 3 blue
checklist

checklist展现给用户一系列选项,可以用space键来选择。

dialog --checklist "Choose toppings:" 10 40 3 \
        1 Cheese on \
        2 "Tomato Sauce" on \
        3 Anchovies off

第三列是初始状态,可以是on或者off。

radiolist

和checklist很像,但radiolist要求用户必须选择一个选项,且只能选择一个。

dialog --backtitle "CPU Selection" \
  --radiolist "Select CPU type:" 10 40 4 \
        1 386SX off \
        2 386DX on \
        3 486SX off \
        4 486DX off

一个简单的例子如下:

#!/bin/bash

# inputbox - demonstrate the input dialog box with a temporary file

# 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}

# Create a temporary file and make sure it goes away when we're dome
tmp_file=$(tempfile 2>/dev/null) || tmp_file=/tmp/test$$
trap "rm -f $tmp_file" 0 1 2 5 15

# Generate the dialog box
dialog --title "INPUT BOX" \
  --clear  \
  --inputbox "Hi, this is an input dialog box. You can use \n
this to ask questions that require the user \n
to input a string as the answer. You can \n
input strings of length longer than the \n
width of the input box, in that case, the \n
input field will be automatically scrolled. \n
You can use BACKSPACE to correct errors. \n\n
Try entering your name below:" 16 51 2> $tmp_file

# Get the exit status
return_value=$?

# Act on it
case $return_value in
  $DIALOG_OK)
    echo "Result: `cat $tmp_file`";;
  $DIALOG_CANCEL)
    echo "Cancel pressed.";;
  $DIALOG_HELP)
    echo "Help pressed.";;
  $DIALOG_EXTRA)
    echo "Extra button pressed.";;
  $DIALOG_ITEM_HELP)
    echo "Item-help button pressed.";;
  $DIALOG_ESC)
    if test -s $tmp_file ; then
      cat $tmp_file
    else
      echo "ESC pressed."
    fi
    ;;
esac

更多例子参考网络文章

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容