2015-06-04 46 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 
相關問題