shell脚本实战(第2版)/人民邮电出版社 脚本33 处理压缩文件

Z="compress";	unZ="uncompress";	Zlist=""
gz="gzip"	ungz="ungzip";		gzlist=""
bz="bzip2"	unbz="bunzip2"		bzlist=""

for arg do
	if [ -f "$arg" ];then
		case	"$arg"	in
			*.Z)	$unZ	"$arg"
				arg="$(echo $arg | sed 's/\.Z$//')"
				Zlist="$ZList \"$arg\"" 
				;;
			*.gz)	$ungz	"$arg"
				arg="$(echo $arg | sed 's/\.gz$//')"
				gzlist="$gzList \"$arg\"" 
				;;
			*.bz2)	$unbz	"$arg"
				arg="$(echo $arg | sed 's/\.bz2$//')"
				bzlist="$bzlist\"$arg\""
				;;
		esac
	fi
	newargs="${newargs:-""} \"$args\""
done


case $0 in
	*zcat*)	eval	cat 	$newargs	;;
	*zmore*) eval 	more	$newargs	;;	
	*zgrep*) eval	grep	$newargs	;;	
	*)	echo "$0:unknown base name.Can't preceed.">&2
		exit1
esac

if [ ! -z "$list" ];then
	eval $Z $Zlist
fi

if [ ! -z "$gzlist" ];then
	eval $gz $gzlist
fi
if [ ! -z "$bzlist" ];then
	eval $bz $bzlist
fi
exit 0

运行结果

shell脚本实战(第2版)/人民邮电出版社 脚本33 处理压缩文件

 

上一篇:Shell编程之条件语句


下一篇:十一、shell编程-正则表达式