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''


















本文转自丁丁历险51CTO博客,原文链接:http://blog.51cto.com/manual/1064577 ,如需转载请自行联系原作者


相关文章
|
2月前
|
弹性计算 运维 Shell
使用 shell 脚本打印图形
【4月更文挑战第29天】
43 1
|
2月前
|
弹性计算 运维 Shell
使用shell 脚本打印图形3
【4月更文挑战第29天】
34 0
|
2月前
|
存储 弹性计算 运维
使用shell 脚本打印图形2
【4月更文挑战第29天】
29 0
|
2月前
|
弹性计算 运维 Shell
使用shell 脚本打印图形1
【4月更文挑战第29天】
33 0
|
2月前
|
Shell Linux
Linux|shell编程|拷贝大文件之显示进度条
Linux|shell编程|拷贝大文件之显示进度条
77 0
|
12月前
|
Shell Python
Python: shell上面的进度条打印是如何实现的?
Python: shell上面的进度条打印是如何实现的?
105 1
|
Shell
shell编程之双重循环(教你花式打印各种图形)(下)
1、双重循环概述 双重循环需要具备的前提——存在两个以上的自变量。 执行机制:
254 0
|
Shell
shell编程之双重循环(教你花式打印各种图形)(上)
1、双重循环概述 双重循环需要具备的前提——存在两个以上的自变量。 执行机制:
637 0