如果从子 shell 给定一个空文件列表,则使 'awk' 退出
Make 'awk' exit if it is given an empty file list from a subshell
我运行:
find mydir -type f -name "the_thing.txt"
但我什么也没得到(文件不存在)。
那我运行:
awk '{print [=12=]}' $(find mydir -type f -name "the_thing.txt")
然后我将 shell 卡在 awk
中(因为未指定输入文件,并且 awk
现在正在等待标准输入)。
如何让 awk
(或 cat
)不打印任何内容并退出,以防 find
不输出任何内容?
您之前的 post 包括 -maxdepth 1
选项,它使文件路径统一化。
这就是我问这个的原因。现在该选项已删除,我明白了 some subdirectory
.
的意思
那你试试看:
find mydir -type f -name "the_thing.txt" -print0 | xargs -0 -r awk '{print [=10=]}'
请注意,xargs
的 -r
选项会在输入为空时抑制执行。
如果你想用head
命令限制文件最多为一个,你可以说:
find mydir -type f -name "the_thing.txt" -print0 | head -n1 -z | xargs -0 -r awk '{print [=11=]}'
head
的 -z
选项在 coreutils 8.25
(2016 年 1 月左右)中引入。
如果您的head
命令不支持该选项,请换句话说:
find mydir -type f -name "the_thing.txt" | head -n1 | xargs -r awk '{print [=12=]}'
这对包含空白字符的文件名不太稳健。
我运行:
find mydir -type f -name "the_thing.txt"
但我什么也没得到(文件不存在)。
那我运行:
awk '{print [=12=]}' $(find mydir -type f -name "the_thing.txt")
然后我将 shell 卡在 awk
中(因为未指定输入文件,并且 awk
现在正在等待标准输入)。
如何让 awk
(或 cat
)不打印任何内容并退出,以防 find
不输出任何内容?
您之前的 post 包括 -maxdepth 1
选项,它使文件路径统一化。
这就是我问这个的原因。现在该选项已删除,我明白了 some subdirectory
.
那你试试看:
find mydir -type f -name "the_thing.txt" -print0 | xargs -0 -r awk '{print [=10=]}'
请注意,xargs
的 -r
选项会在输入为空时抑制执行。
如果你想用head
命令限制文件最多为一个,你可以说:
find mydir -type f -name "the_thing.txt" -print0 | head -n1 -z | xargs -0 -r awk '{print [=11=]}'
head
的 -z
选项在 coreutils 8.25
(2016 年 1 月左右)中引入。
如果您的head
命令不支持该选项,请换句话说:
find mydir -type f -name "the_thing.txt" | head -n1 | xargs -r awk '{print [=12=]}'
这对包含空白字符的文件名不太稳健。