Shell脚本一枚

脚本如下:

#!/bin/bash
dir=$1
dir1=""
for file in `find $dir -type f`
do
dir2=${file%/*}
name=${file%.*}
name1=${name%.*}
cd $dir2
gunzip $file
if [ -z "$dir1" -o "$dir1" != "$dir2" ];then
gzip -c "$name" > "$name1".gz
rm -rf "$name"
dir1="$dir2"
else
gzip -c "$name" >> "$name1".gz
rm -rf "$name"
fi
done

需要说明的是:

1. ${file%/*}:拿掉最后一个 / 及其右边的字符串

2. ${file%.*}:拿掉最后一个 .  及其右边的字符串

3. 第一个gzip是将file1压缩成foo.gz,第二个gzip是将file2添加到foo.gz中

gzip -c file1 > foo.gz
    gzip -c file2 >> foo.gz

 

上一篇:POCO库中文编程参考指南(8)丰富的Socket编程


下一篇:第一章-第七题( 有人认为,“中文编程”, 是解决中国程序员编程效率一个秘密武器,请问它是一个 “银弹” 么? )--By 侯伟婷