Bash 查找不包括子目录的命令
对不起,其他问题对我没有帮助:/ 我想在一个目录上循环,以便在某些文件上应用程序。 问题是它还包括子目录。 我的代码怎么了Bash 查找不包括子目录的命令,bash,loops,command,Bash,Loops,Command,对不起,其他问题对我没有帮助:/ 我想在一个目录上循环,以便在某些文件上应用程序。 问题是它还包括子目录。 我的代码怎么了 #!/bin/bash for file in `find $path/* \( -name "*.txt" -o -name "*.out" \) -prune -maxdepth 1 -type f `; do program "$file" -outdir=$outdir; do
#!/bin/bash
for file in `find $path/* \( -name "*.txt" -o -name "*.out" \) -prune -maxdepth 1 -type f `;
do
program "$file" -outdir=$outdir;
done
不要将
find
的输出用作for
循环中的列表。当文件名包含空白字符时,它将不起作用,而且无论如何,为该作业调用外部命令是多余的。改用globs:
#!/bin/bash
shopt -s nullglob
for file in "$path"/*.txt "$path"/*.out; do
program "$file" -outdir="$outdir"
done
注:
- 启用
选项后,当未找到与模式匹配的文件时,会导致从列表中删除模式。否则,模式将保持不变nullglob
可以简化为“$path”/*.txt”$path”/*.out
“$path”/{*.txt,*.out}
- 上面的模式与以点(
)开头的文件名不匹配,但如果需要,可以进行补救 - 与模式匹配的文件不必是常规文件。最好通过
,在[[-f“$file”]| | continue
循环的主体顶部,检查这一点for
find
的目的就是遍历子目录。