歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Shell腳本應用圖形界面的案例

Shell腳本應用圖形界面的案例

日期:2017/3/3 16:37:12   编辑:關於Linux

在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

效果如圖:

Copyright © Linux教程網 All Rights Reserved