今天玩的时候,我试图让 grep 消耗......的输出
$(apt-cache rdepends hunspell-fr | sed -e 's/^\s*|\?//' -e 1s/^/\'/g -e \$s/$/\'/)
...作为嵌套在单引号内的换行符分隔模式列表。当我运行上面的命令,然后简单地将结果输出复制/粘贴到新的 grep 命令中时,它的行为符合预期......
nohatsatthetable@debian:~$ dpkg -l | grep 'hunspell-fr
Reverse Depends:
hunspell-fr-classical
thunderbird-l10n-fr
firefox-esr-l10n-fr
thunderbird-l10n-fr
task-french-desktop
hunspell-fr-revised
hunspell-fr-revised
hunspell-fr-comprehensive
hunspell-fr-comprehensive
hunspell-fr-classical
firefox-esr-l10n-fr'
ii firefox-esr-l10n-fr 102.4.0esr-1~deb11u1 all French language package for Firefox ESR
ii hunspell-fr 1:7.0-1 all French dictionary for hunspell (dependency package)
ii hunspell-fr-classical 1:7.0-1 all French dictionary for hunspell (classical version)
ii task-french-desktop 3.68+deb11u1 all French desktop
然而,当我运行以下单行代码时(在我看来,它在功能上应该是相同的),grep 仅将第一行解释为模式,将所有后续行(或第二行情况下的单词)解释为要在其中搜索模式的目录或文件...
nohatsatthetable@debian:~$ dpkg -l | grep $(apt-cache rdepends hunspell-fr | sed -e 's/^\s*|\?//' -e 1s/^/\'/g -e \$s/$/\'/)
grep: Reverse: No such file or directory
grep: Depends:: No such file or directory
grep: hunspell-fr-classical: No such file or directory
grep: thunderbird-l10n-fr: No such file or directory
grep: firefox-esr-l10n-fr: No such file or directory
grep: thunderbird-l10n-fr: No such file or directory
grep: task-french-desktop: No such file or directory
grep: hunspell-fr-revised: No such file or directory
grep: hunspell-fr-revised: No such file or directory
grep: hunspell-fr-comprehensive: No such file or directory
grep: hunspell-fr-comprehensive: No such file or directory
grep: hunspell-fr-classical: No such file or directory
grep: firefox-esr-l10n-fr': No such file or directory
为什么它会这样?
答案1
是的,正如@提到的善行难陀 ,引号是强制性的:
$ dpkg -l |
grep "$(apt-cache rdepends hunspell-fr |
sed -e 's/^\s*|\?//' -e 1s/^/\'/g -e \$s/$/\'/
)"