我正在编写一个bash脚本,用于检查特定fileName.log是否存在tar存档,如果不存在,则使用fileName.log创建一个。如果tar已经存在,那么我需要将fileName.log附加到它。我从来没有真正使用tar档案,除了解压缩和解压缩已经给我的.tar.gz文件。我确定我的问题是我的语法,但是我无法根据手册页找出正确的语法。
我的代码:
# check if tarball for this file already exists. If so, append it. If not, create new tarball
if [ -e "$newFile.tar" ];
then
echo "tar exists"
tar -cvf "$newFile" "$newFile.tar"
else
echo "no tar exists"
tar -rvf "$newFile"
fi
答案 0 :(得分:1)
如果您想以这种方式将$newfile
添加到$newfile.tar
:
if [ -f "$newFile.tar" ];
then
echo "tar exists"
tar -rvf "$newFile.tar" "$newFile"
else
echo "no tar exists"
tar -cvf "$newFile.tar" "$newFile"
fi
答案 1 :(得分:1)
非常接近,您的-c
和-r
标记已反转(c
已创建,r
附加)并且您希望首先放置文件名,如下所示:< / p>
if [ -e "$newFile.tar" ];
then
echo "tar exists"
tar -rvf "$newFile.tar" "$newFile"
else
echo "no tar exists"
tar -cvf "$newFile.tar" "$newFile"
fi