这是根据您的说明修改后的脚本。我已经使用tar
压缩gzip
文件将多个文件存储在一个存档中(您不能gzip
单独使用存储多个文件)。这段代码只是表面上的测试——它可能有一个或两个错误,如果你在愤怒中使用它,你应该添加更多的代码来检查命令是否成功等。但它应该能让你大部分时间到达那里。
#!/bin/bash
oldfile=$1
newfile=$2
month=`date +%B`
year=`date +%Y`
prefix="frozenskys"
archivefile=$prefix.$month.$year.tar
# Check for existence of a compressed archive matching the naming convention
if [ -e $archivefile.gz ]
then
echo "Archive file $archivefile already exists..."
echo "Adding file '$oldfile' to existing tar archive..."
# Uncompress the archive, because you can't add a file to a
# compressed archive
gunzip $archivefile.gz
# Add the file to the archive
tar --append --file=$archivefile $oldfile
# Recompress the archive
gzip $archivefile
# No existing archive - create a new one and add the file
else
echo "Creating new archive file '$archivefile'..."
tar --create --file=$archivefile $oldfile
gzip $archivefile
fi
# Update the files outside the archive
mv $newfile $oldfile
将其另存为script.sh
,然后使其可执行:
chmod +x script.sh
然后像这样运行:
./script.sh oldfile newfile
类似 , 的东西frozenskys.September.2009.tar.gz
将被创建,newfile
并将替换oldfile
. 如果需要,您也可以exec
从另一个脚本调用此脚本。只需将此行放入您的第二个脚本中:
exec ./script.sh $1 $2