图形化桌面环境中的脚本编程
发布日期:2021-06-30 15:02:57 浏览次数:2 分类:技术文章

本文共 3982 字,大约阅读时间需要 13 分钟。

contents

使用字符打印的菜单界面实现交互。

1. 创建文本菜单

通常菜单脚本会清空显示区域、显示可用选项列表、识别用户选择,这就意味着文本菜单的核心是 case 命令。

1.1 创建菜单布局

clear 命令可用于当前终端会话的 terminfo 数据来清理出现在屏幕上的文本。

echo 默认只显示可打印字符,-e 选项能够使其显示制表符,-en 选项能够去掉末尾的换行符。

function menu {
clear echo echo -e "\t\t\tSys Admin Menu\n" echo -e "\t1. Display disk space" echo -e "\t2. Display logged on users" echo -e "\t3. Display memory usage" echo -e "\t0. Exit menu\n\n" echo -en "\t\t Enter option: " # 去掉行尾回车}

创建菜单的最后一步是获取用户输入,这步用 read 命令,在 read 命令中使用 -n 选项来限制只读取一个字符。这样用户只需要输入一个数字,也不用按回车键:

read -n 1 option # 读取 1 个字符

1.2 创建菜单函数

我们需要为每个菜单选项提供独立的 shell 函数。创建 shell 菜单脚本的第一步就是决定你希望脚本执行哪些功能,然后将这些功能以函数的形式放在代码中。

为还没实现功能的函数创建一个桩函数,可以是一个空函数,也可以是具有 echo 语句的函数。

function diskspace {
clear echo "This is where the diskspace commands will go."}

1.3 添加菜单逻辑

使用 case 命令:

menucase $option in	0)		break ;;	1)		diskspace ;;	2)		whoseon ;;	3)		memusage ;;	*)		clear		echo "Sorry, wrong selection" ;;esac

1.4 整合 shell 脚本菜单

#!/bin/bash#...function diskspace {
clear df -k}function whoseon {
clear cat /proc/meminfo}function memusage {
clear cat /proc/meminfo}function menu {
clear echo echo -e "\t\t\tSys Admin Menu\n" echo -e "\t1. Display disk space" echo -e "\t2. Display logged on users" echo -e "\t3. Display memory usage" echo -e "\t0. Exit menu\n\n" echo -en "\t\t Enter option: "}while [ 1 ]do menu case $option in 0) break ;; 1) diskspace ;; 2) whoseon ;; 3) memusage ;; *) clear echo "Sorry, wrong selection" ;; esac echo -en "\n\n\t\t\tHit any key to continue" read -n 1 linedoneclear

1.5 使用 select 命令

使用 select 快速创建出菜单,然后获取输入的答案并自动处理。

select variable in listdo 	commandsdone

list:由空格分隔的文本选项列表,这些列表构成了整个菜单,select 命令会将每个列表项显示成一个带编号的选项,然后为选项显示一个由PS3环境变量定义的特殊提示符。

#!/bin/bashfunction diskspace {
clear df -k}function whoseon {
clear who}function memusage {
clear cat /proc/meminfo}PS3="Enter options: " select option in "Display disk space" "Display logged on users" "Display memory usage" "Exit program"do case $option in "Exit program") break ;; "Display disk space") diskspace ;; "Display memory usage") memusage ;; "Display logged on users") whoseon ;; *) clear echo "Sorry, wrong selection" ;; esacdoneclear

2. 制作窗口

2.1 dialog 包

在这里插入图片描述

dialog --widget parameters

  • widget:表中部件名称。
  • parameters:部件窗口的大小和文本。

msgbox 部件

dialog --title text --msgbox text height width

yesno 部件

dialog --title "Please answer" --yesno "Is this thing on" 10 20
echo $? # 查看选择 yes:0,no:1

inputbox 部件

dialog 命令会将文本字符串的值发送给 STDERR,必须重定向 STDERR 来获取用户输入。
``
textbox 部件
dialog --inputbox "Enter your age:" 10 20 2>age.txt # 将对文本框中的输入重定向到 age.txt

0 是一个文件描述符,表示标准输入(stdin)

1 是一个文件描述符,表示标准输出(stdout)
2 是一个文件描述符,表示标准错误(stderr)

menu 部件

dialog --menu "Sys Admin Menu" 20 30 10 1 "Displa disk space" 2 "Display users" 3 "Display memory usage" 4 "Exit" 2>text.txt
在这里插入图片描述
fselect 部件
选择文件。
dialog --title "Select a file" --fselect $HOME/ 10 50 2>file.txt
在这里插入图片描述
cat file.txt # /home/jiaming/Documents/menu.sh

2.2. dialog 选项

在这里插入图片描述

2.3 在脚本中使用 dialog 命令

#!/bin/bashtemp=$(mktemp -t test.XXXXXX) # mktemp 命令创建两个临时文件来保存 dialog 命令的数据。temp2=$(mktemp -t test2.XXXXX)function diskspace {
df -k > $temp dialog --textbox $temp 20 60}function whoseon {
cat /proc/meminfo > $temp dialog --textbox $temp 20 50}function memusage {
cat /proc/meminfo > $temp dialog --textbox $temp 20 50}while [ 1 ]do dialog --menu "Sys Admin Menu" 20 30 10 1 "Display disk space" 2 "Display users" 3 "Display memory usage" 0 "Exit" 2>$temp2 # 执行完每个函数后,脚本都会返回继续显示菜单。 if [ $? -eq 1 ] # 检查 dialog 命令的退出状态码,防止用户按下 cancel 按钮退出。 then break fi selection=$(cat $temp2) case $selection in 1) diskspace ;; 2) whoseon ;; 3) memusage ;; 0) break ;; *) dialog --msgbox "Sorry, invalid selection" 10 30 esacdonerm -f $temp 2> /dev/nullrm -f $temp2 2> /dev/null

3. 使用图形

3.1 KDE 环境

3.1.1 kdialog 部件

3.1.2 使用 kdialog

3.2 GNOME 环境

3.2.1 zenity 部件

3.2.2 在脚本中使用 zenity

转载地址:https://jiaming.blog.csdn.net/article/details/104919983 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:第六章:内置模块(Ⅱ)
下一篇:创建函数

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月30日 20时38分54秒