shell脚本实战(第2版)/人民邮电出版社 脚本34 确保最大化压缩文件

Z="compress"	gz="gzip"	bz="bzip2"
Zout="/tmp/bestcompress.$$.Z"
gzout="/tmp/bestcompress.$$.gz"
bzout="/tmp/bestcompress.$$.bz"

skipcompressed=1

if [ "$1" = "-a" ];then
	skipcompressed=0;shift
fi

if [ $# -eq 0 ];then
	echo "Usage:$0 [-a] file or files to optimally compress">$2i
	exit 1
fi

trap "rm -f $Zout $gzout $bzout" EXIT

for name in "$@" 
do
	if [ ! -f "$name" ];then
		echo "$0:file $name not found.Skipped">&2
		continue
	fi

	if [ "$(echo $name | egrep '(\.Z$|\.gz$|\.bz2$)')" != "" ];then
		if [ $skipcompressed -eq 1 ];then
			echo "Skipped file ${name}:It is already compressed."
			continue
		else
			echo "Warning:Trying to doule-compress $name"
		fi
	fi

	$Z < "$name" > $Zout &
	$gz < "$name" > $gzout &
	$bz < "$name" > $bzout &

	wait

	smallest="$(ls -l "$name" $Zout $gzout $bzout | awk '{print $5"="NR}' | sort -n | cut -d= -f2 | head -1)"

	echo "$smallest"

	case "$smallest" in
		1)	echo "No space savings by compressing $name.Left as is."
			;;
		2) 	echo Best compression is with compress.File renamed ${name}.Z
			mv $Zout "${name}.Z"; rm -f "$name"
			;;
		3) 	echo Best compression is with gzip.File renamed ${name}.gz
			mv $gzout "${name}.gz"; rm -f "$name"
			;;	
		4) 	echo Best compression is with bzip2.File renamed ${name}.bz2
			mv $bzout "${name}.bz2"; rm -f "$name"
	esac

done
exit 0

运行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值