unix - 使用 tar 和 grep 命令检查文件是否存在于 tar 文件中

标签 unix ksh tar

<分区>

我有一个脚本可以检查一个文件是否存在于 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

谢谢。

最佳答案

因为 some_file 位于 tarfile.tar 文件中,但您的 if 正在检查它是否在文件系统上。

你可以这样修改

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

关于unix - 使用 tar 和 grep 命令检查文件是否存在于 tar 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508381/

相关文章:

linux - 安装失败

bash - 在 Unix 中将 zip 转换为存储压缩

c++ - 如何使用 QMake 文件获取 Valgrind 的调试符号

linux - 如何 gzip 文件同时将压缩内容打印到标准输出

c - 使用 unix 域套接字编写单独的消息

unix - ksh:如何探测标准输入?

sql - Oracle PL/SQL : Dump query result into file

c++ - 如何获取 tar 中存档的文件(及其他属性)的创建时间?

python - 用python组织tar bz2文件中的文件

linux - 使用 tar 命令时如何防止覆盖现有文件