2015-06-04 53 views
0

我正在编写一个bash脚本来检查特定的fileName.log是否存在tar归档文件,如果没有,则使用fileName.log创建一个。如果一个tar已经存在,那么我需要将fileName.log添加到它。除了解压缩和解压缩已经提供给我的.tar.gz文件之外,我从来没有真正使用过tar档案。我确定我的问题是我的语法,但我无法根据手册页找出正确的语法。使用单个文件在当前目录中创建新的tar文件

我的代码:

 # 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 

回答

1

相当接近,你有你的-c-r标志反转(c创建,r追加),并且想要首先输入文件名,如下所示:

if [ -e "$newFile.tar" ]; 
then 
    echo "tar exists" 
    tar -rvf "$newFile.tar" "$newFile" 
else 
    echo "no tar exists" 
    tar -cvf "$newFile.tar" "$newFile" 
fi 
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 
相关问题