天天看點

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中提供進度的顯示。該腳本有兩個參數,第一個參數為源檔案路徑,第二個參數為目标路徑。如果您的應用案例不同可以據此稍作修改即可使用。

#!/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 ... 

EOF 

/bin/cp -r $file $des &>/dev/null 

  done | dialog --title "Copy" --gauge "files" 6 70 

clear 

效果如圖:

<a href="http://blog.51cto.com/attachment/201211/110856502.gif" target="_blank"></a>

 丁丁曆險http://manual.blog.51cto.com/3300438/1064577''

本文轉自丁丁曆險51CTO部落格,原文連結:http://blog.51cto.com/manual/1064577 ,如需轉載請自行聯系原作者