专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

用shell脚本显示文件下载进度功能

百度收藏 QQ搜藏

这个shell脚本通过在一个GTK+对话框(使用小巧方便的对话框工具zenity,下载地址是http://download.chinaunix.net/download/0013000/12216.shtml),来显示文件下载了百分之多少(%)。当我们在命令行下使用shell命令,比如用wget下载文件再配合这个脚本将是非常有用的。

相信该shell脚本是很有帮助的,代码如下:

#!/bin/sh
#http://hektor.umcs.lublin.pl/~mikosmul/computing/shell-scripts/view-download-progress
# Written by Michal Kosmulski <mkosmul _at_ users _dot_ sourceforge _dot_ net>
# This script is hereby put in the public domain.
# Display a dialog box with a progress bar showing how much of a file
# has been downloaded.
# Parameters:
# $1 file name
# $2 final file size (in bytes)
# Uses zenity
if [ $# != 2 ]; then
 echo "Usage:"
 echo "$0 file_name final_size"
 exit 1;
fi
name="$1"
final_size="$2"
size=0
(
 while (( size < final_size )); do
  if [ -f "$name" ]; then
   size=`ls -l "$name" | awk '{print $5}'`;
  else
   size=0
  fi
  echo -e '#'"${name}: ${size} of ${final_size}";
  echo -e "100*${size}/${final_size};\nquit\n" | bc;
  sleep 1;
 done
 echo '#'"${name}: Finished downloading - ${size} of ${final_size}";
) | zenity --title="Downloading file ${name}" --progress --percentage=0

上一篇:shell程序:读取两个输入数字并显示中间的奇数 下一篇:加密文本成二进制内容的shell脚本

power by soyo123 2007-2008