歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> shell之圖形進度條

shell之圖形進度條

日期:2017/3/1 10:06:08   编辑: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. # Read the parameter for copy,$1 is source dir and $2 is destination dir
  6. dir=$1/*
  7. des=$2
  8. # Test the destination dirctory whether exists
  9. [ -d $des ] && echo "Dir Exist" && exit 1
  10. # Create the destination dirctory
  11. mkdir $des
  12. # Set counter, it will auto increase to the number of source file
  13. i=0
  14. # Count the number of source file
  15. n=`echo $1/* |wc -w`
  16. for file in `echo $dir`
  17. do
  18. # Calculate progress
  19. percent=$((100*(++i)/n))
  20. cat <<EOF
  21. XXX
  22. $percent
  23. Copying file $file ...
  24. XXX
  25. EOF
  26. /bin/cp -r $file $des &>/dev/null
  27. done | dialog --title "Copy" --gauge "files" 6 70
  28. clear

效果如圖:

Copyright © Linux教程網 All Rights Reserved