Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 查找不包括子目录的命令_Bash_Loops_Command - Fatal编程技术网

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
    循环的主体顶部,检查这一点

非常感谢您!我显然太瞎了!:D最简单的解决方案是最好的。
find
的目的就是遍历子目录。