2013-10-22 162 views
2

我有一個腳本,用於檢查文件是否存在於tar文件中,但是由於它總是轉到腳本的「其他」部分,所以出現錯誤。我非常肯定它不應該那樣。使用tar和grep命令檢查tar文件裏面是否存在文件

日期爲「Mon dd」格式(1月11日)。

echo "enter date: \c" 
read date 
tarfile=`tar -tvf tarfile.tar | grep some_file | grep "$date"` 

if [ -f "$tarfile" ]; then 
    echo "yes" 
else 
    echo "no" 
fi 

謝謝。

+0

這個問題似乎是題外話,因爲也有人在這裏問:http://unix.stackexchange.com/questions/97068/tar-and-if - 陳述和回答,所以我正在關閉這個變體。 –

回答

7

由於some_file位於tarfile.tar文件中,但您的if正在檢查它是否位於文件系統中。

你可以改變它像這樣

if tar –tf tarfile.tar some_file >/dev/null 2>&1; then 
    echo "tarfile.tar contains some_file" 
fi