我有一组三个文件,分布在大约 90 个子目录中。我正在尝试运行 for 循环来根据特定文件集的存在/不存在来执行命令。这有多个条件,这是一个例子-
for subj in `cat sublist.txt`; do
if [ -a ${subj}/${subj}*file1 && -a ${subj}/${subj}*file2 ]; then
blah blah...
elif [ -a ${subj}/${subj}*file1 && -a ${subj}/${subj}*file3 ]; then
blah blah...
elif [ -a ${subj}/${subj}*file2 && -a ${subj}/${subj}*file3 ]; then
balh blah...
elif [ -a ${subj}/${subj}*file1 ! -a ${subj}/${subj}*file2 && ! -a ${subj}/${subj}*file3 ]; then
blah blah...
elif [ -a ${subj}/${subj}*file2 ! -a ${subj}/${subj}*file1 && ! -a ${subj}/${subj}*file3 ]; then
blah blah
else blah blah
fi
done
我不断收到语法错误 - bash: [: Missing `]'
我尝试使用 -[[ ]] 运行它并检查空格中的错误。 我的脚本可能有什么问题?我该如何纠正才能为我的命令提供正确的输入文件?
提前感谢您的反馈/建议
考虑使用
bash
的双方括号测试:
path1=$(compgen -G ${subj}/${subj}*file1) path2=$(compgen -G ${subj}/${subj}*file2)
if [[ -s $path1 && -s $path2 ]]; then
...
[
或 test
是 POSIX
测试命令。它可以对文件和字符串进行简单的测试。在 bash
中,您应该使用更强大的 [[
来代替,并为了一致性而禁止 [
。 [[
可以进行模式匹配,使用起来更快更安全。
看
http://mywiki.wooledge.org/BashGuide/TestsAndConditionals
compgen -G glob*
是扩展 glob 的技巧。您不能在这样的测试中使用通配符。