8

图形环境下的shell编程

 3 years ago
source link: https://www.lujun9972.win/blog/2016/12/01/%E5%9B%BE%E5%BD%A2%E7%8E%AF%E5%A2%83%E4%B8%8B%E7%9A%84shell%E7%BC%96%E7%A8%8B/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

图形环境下的shell编程

select

select命令可以创建很简单的菜单,然后获取输入的答案.

select命令的格式为:

select variable in list
do
    commands
done

其中list参数是由空格分隔的文本选项列表,这些列表构成了整个菜单. select命令会将每个列表现实成一个带编号的选项,然后为选项显示一个由PS3环境变量定义的提示符.

例如我们可以创建一个简单的文件选择脚本:

if [ $# -eq 0 ];then
    file=$(pwd)
else
    file=$1
fi

if [ -d $file ];then
    select f in $(ls $file)
    do
        exec $0 $file/$f
    done
else
    echo $file
fi
lujun9972@lujun9972-desktop:~$ file_selector.sh bin
 1) -          17) git-sync.sh
 2) autoload         18) mail2org.sh
 3) cleanFiles.pl      19) mgrep.sh
 4) develop        20) monitor_network.sh
 5) dired.sh         21) mpg123.sh
 6) download_comic_from_avbebe.sh  22) my-byte-split.sh
 7) ec           23) my-line-split.sh
 8) ediff.sh         24) pick.sh
 9) emacsclient.sh       25) ptree.sh
10) emages.sh        26) rcsvi.sh,v
11) es           27) redo.sh
12) eshell.sh        28) shr
13) et           29) testClean.cfg
14) file_selector.sh       30) vir
15) gclsg.lisp.tw      31) wget-website.sh
16) git-push.sh
#? 2
1) git-pull.sh
2) study.sh
#? 1
bin/autoload/git-pull.sh

dialog

dialog能够使用ANSI转义控制字符从文本环境创建标准的窗口对话框.

下面是用用dialog来重写的file_selector.sh

if [ $# -eq 0 ];then
    file=$(pwd)
else
    file=$1
fi

file=$(dialog --title "file selector" --stdout --fselect $file 10 50)

if [ -d $file ];then
    exec $0 $file
else
    echo $file
fi

screenshot-20161206-075142.png

tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作.

通过使用 tput,您可以更改几项终端功能:如移动或更改光标、更改文本属性,以及清除终端屏幕的特定区域等.

下面是一个例子:

#!/bin/bash

BOLD=$(tput bold)
REV=$(tput rev)
NORMAL=$(tput sgr0)
CURSOR_OFF=$(tput civis)
CURSOR_ON=$(tput cnorm)

tput init

tput clear
echo $CURSOR_OFF
tput cup 2 15
echo  "${BOLD}粗体效果{NORMAL}\n"
echo  "${REV}反转效果${NORMAL}"
echo $CURSOR_ON

screenshot-20161205-231111.png

notify-send

notify-send让你通过通知进程发送一个桌面通知给用户.

例如下面是一个检测网络的脚本.

#!/bin/bash
target="www.baidu.com"
port=80
interval=60                     # 秒

function live_p()
{
    local remote=$1
    local port=$2
    if timeout 5 echo >/dev/tcp/$remote/$port;then
        echo "网络通了!"
    else
        echo "网络不通了!"
    fi
}

while :
do
    current_state=$(live_p $target $port)
    if [ "$current_state" != "$old_state" ];then
        notify-send $current_state
    fi
    old_state=$current_state;
    sleep $interval
done

网络不通的情况下,它会显示一个窗口告诉你"网络不通了!",网络恢复之后,它又会显示一个窗口告诉你"网络通了"

screenshot-20161206-075250.png

kdialog

kdialog为KDE桌面提供了类似dialog式的标准窗口. 其生成的窗口能和其他KDE窗口很好的融合.

关于kdialog的使用方法可以参见https://techbase.kde.org/Development/Tutorials/Shell_Scripting_with_KDE_Dialogs

kdialog的file_selector.sh可以是这样的

if [ $# -eq 0 ];then
    file=$(pwd)
else
    file=$1
fi


if [ -d $file ];then
    file=$(kdialog  --getopenfilename $file)
fi
echo $file

screenshot-20161206-074957.png

zenity

zenity 是gnome版的dialog. 它针对不同的对话框选项有不同的参数:

对话框选项

--calendar显示日历对话框 --entry显示文字输入栏对话框 --error显示错误信息对话框 --file-selection显示文件选择对话框 --info显示信息对话框 --list显示清单对话框 --progress显示进度指示窗 --question显示提问信息对话框 --text-info显示文字资信对话框 --warning显示警告信息对话框 --title=标题指定对话框的标题 --window-icon=图示路径指定视窗使用的图示 --width=宽度 --height=高度 --text=STRING指定对话框中的文字 --day=INT指定日历中的日期 --month=INT指定日历中的月份 --year=INT指定日历中的年份 --date-format=STRING指定回传的日期格式

文字输入栏选项

--text=STRING指定对话框中的文字 --entry-text=STRING指定输入栏中的文字 --hide-text隐藏输入栏中的文字

错误信息选项

--text=STRING指定对话框中的文字

有关文件选择的选项

--filename=文件名称指定文件名称 --multiple允许同时选择多个文件 --separator=分隔字符指定分隔输出结果的字符。 --text=STRING指定对话框中的文字 --column=STRING指定栏位标题 --checklist第一栏使用独立选择按钮 --radiolist第一栏使用多项选择按钮 --separator=分隔字符指定分隔输出结果的字符 --editable可以更改文字 --text=STRING指定对话框中的文字 --percentage=INT指定开始时的百份比数值 --pulsate填满进度列 --auto-close当进度达到100% 时关闭对话框 --text=STRING指定对话框中的文字 --filename=文件名称从文件中载入文本 --editable可以更改文字

警告信息选项

--text=STRING指定对话框中的文字

GTK+ 选项

--gdk-debug=标签准备设定的gdk 调试标签 --gdk-no-debug=标签准备去除的gdk 调试标签 --display=画面准备使用的 X 画面 --sync进行 X 同步呼叫 --name=名称视窗总管所需的程式名称 --class=类别视窗总管所需的程式类别名称

--gxid-host=主机 --gxid-port=端口号

--gtk-debug=标签准备设定的gtk+ 调试标签 --gtk-no-debug=标签准备去除的gtk+ 调试验标签 --g-fatal-warnings将所有警告信息作为严重错误处理 --gtk-module=模块载入额外的 Gtk 模块

zennity版本的file_selector.sh是这样的

if [ $# -eq 0 ];then
    file=$(pwd)
else
    file=$1
fi


if [ -d $file ];then
    cd $file
    file=$(zenity  --file-selection)
fi
echo $file

screenshot-20161206-075045.png


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK