shell之图形进度条

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中提供进度的显示。该脚本有两个参数,第一个参数为源文件路径,第二个参数为目标路径。如果您的应用案例不同可以据此稍作修改即可使用。

 
 
  1. #!/bin/bash 
  2. #Description: A shell script to copy parameter1 to parameter2 and Display a progress bar 
  3. #Author:Jacob 
  4. #Version:0.1 beta 
  5.  
  6. # Read the parameter for copy,$1 is source dir and $2 is destination dir 
  7. dir=$1/* 
  8. des=$2 
  9. # Test the destination dirctory whether exists 
  10. [ -d $des ] && echo "Dir Exist" && exit 1 
  11. # Create the destination dirctory 
  12. mkdir $des 
  13. # Set counter, it will auto increase to the number of source file 
  14. i=0 
  15. # Count the number of source file 
  16. n=`echo $1/* |wc -w` 
  17.  
  18. for file in `echo $dir` 
  19.   do 
  20. # Calculate progress 
  21. percent=$((100*(++i)/n)) 
  22. cat <<EOF 
  23. XXX 
  24. $percent 
  25. Copying file $file ... 
  26. XXX 
  27. EOF 
  28. /bin/cp -r $file $des &>/dev/null 
  29.   done | dialog --title "Copy" --gauge "files" 6 70 
  30. clear 

效果如图:

 丁丁历险http://manual.blog.51cto.com/3300438/1064577

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值