これがあなたの説明に基づいて修正されたスクリプトです。tarで圧縮されたアーカイブを使用gzipして、複数のファイルを1つのアーカイブに保存しました(単独で複数のファイルを保存することはできませんgzip)。このコードは表面的にのみテストされています。おそらく1つか2つのバグがあります。怒りで使用している場合は、コマンドの成功などを確認するためにさらにコードを追加する必要があります。しかし、それはあなたをそこへの道のほとんどに連れて行くはずです。
#!/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必要に応じて、別のスクリプトからこのスクリプトを呼び出すこともできます。この行を2番目のスクリプトに入れるだけです。
exec ./script.sh $1 $2