加入收藏 | 设为首页 | 会员中心 | 我要投稿 佛山站长网 (https://www.0757zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Shell脚本应用图形界面的案例

发布时间:2013-04-23 01:26:35 所属栏目:Linux 来源:站长网
导读:在Shell脚本的编写应用中,有时候会需要用到图形界面的案例,比如默认cp拷贝文件为静默模式,无法看到拷贝的进度与百分比。而dialog正是为Shell提供图形界面的
在Shell脚本的编写应用中,有时候会需要用到图形界面的案例,比如默认cp拷贝文件为静默模式,无法看到拷贝的进度与百分比。而dialog正是为Shell提供图形界面的工具,该工具可以为Shell脚本提供各式各样的图形界面,今天为大家介绍的是dialog提供的进度条图形功能。

dialog指令可以单独执行,各式为dialog --title "Copy" --gauge "files" 6 70 10

备注:title表示图形进度条的标题,gauge为正文内容,进度条高度为6,宽度70,显示进度为10%

for i in {1..100} ; do sleep 1; echo $i | dialog --title 'Copy' --gauge 'I am busy!' 10 70 0; done

下面案例中通过统计源文件个数,再据此计算出拷贝文件的百分比,在Shell中提供进度的显示。该脚本有两个参数,第一个参数为源文件路径,第二个参数为目标路径。如果您的应用案例不同可以据此稍作修改即可使用。

#!/bin/bash    
#Description: A shell script to copy parameter1 to parameter2 and Display a progress bar    
#Author:Jacob    
#Version:0.1 beta    
        
# Read the parameter for copy,$1 is source dir and $2 is destination dir    
dir=$1/*    
des=$2    
# Test the destination dirctory whether exists    
[ -d $des ] && echo "Dir Exist" && exit 1    
# Create the destination dirctory    
mkdir $des    
# Set counter, it will auto increase to the number of source file    
i=0    
# Count the number of source file    
n=`echo $1/* |wc -w`    
        
for file in `echo $dir`    
  do
# Calculate progress    
percent=$((100*(++i)/n))    
cat <<EOF    
XXX    
$percent    
Copying file $file ...    
XXX    
EOF    
/bin/cp -r $file $des &>/dev/null
  done | dialog --title "Copy" --gauge "files" 6 70    
clear

效果如图:

(编辑:佛山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读