bee*_*bee 3 scripting tar ksh files
我有一个脚本来检查一个文件是否存在于 tar 文件中,但出了点问题,因为它总是转到else
脚本的一部分。我很确定它不应该是那样的。
日期采用“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
Run Code Online (Sandbox Code Playgroud)
您正在使用 来检查文件是否存在-f
,但这不是您想要做的。该文件存在于 tar 文件中,但-f
无法自行读取 tar 档案中的内容。例如,如果您的文件位于 tar 文件中的“foo/bar”,它将查找与您当前目录相关的“foo/bar”,而该目录不存在。
更好的方法是只检查 的退出状态grep
,而不是尝试解析输出。
printf 'enter date: '
read date
if tar -tvf tarfile.tar | grep some_file | grep -q "$date"; then
echo yes
else
echo no
fi
Run Code Online (Sandbox Code Playgroud)