2013-10-22 68 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