简体   繁体   English

Glob 模式适用于 shell 命令,但不适用于 Perl 反引号

[英]Glob pattern works in shell command but not in Perl backticks

I have 3 files in 3 different directories and I need to printout only files from DIR 1 and 2我在 3 个不同的目录中有 3 个文件,我只需要打印来自 DIR 1 和 2 的文件

1 /tmp/CDE/fileA.log
2 /tmp/CFGH/fileB.log
3 /tmp/CILM_NO/fileC.log 

if I run from bash /bin/ls /tmp/C{[AZ][AZ],[AZ][AZ][AZ]}/*.log it works and I get:如果我从 bash /bin/ls /tmp/C{[AZ][AZ],[AZ][AZ][AZ]}/*.log它,我得到:

/tmp/CDE/fileA.log  /tmp/CFGH/fileB.log

if I run ls bash command from script perl:如果我从脚本 perl 运行 ls bash 命令:

$cmd=`/bin/ls /tmp/C{[A-Z][A-Z],[A-Z][A-Z][A-Z]}/*.log`;
chomp($cmd);
print "$cmd\n";

I receive:我收到:

/bin/ls: cannot access /tmp/C{[A-Z][A-Z],[A-Z][A-Z][A-Z]}/*.log: No such file or directory

It looks I need to escape \{ or \, or \} but got the same output and it does not work I also tried using quote instead of escaping but still got same error output It's not a matter of permission, script is 777看起来我需要转义\{ or \, or \}但得到相同的 output 并且它不起作用我也尝试使用引号而不是 escaping 但仍然得到同样的错误 Z78E6221F6393D13566871DB398F14CE6 不是权限问题

Can't sort of it.搞不定啊

(not an answer, an explanation) (不是答案,是解释)

In your shell在您的 shell

ls /tmp/C{[A-Z][A-Z],[A-Z][A-Z][A-Z]}/*.log

That uses bash Brace Expansion .那使用 bash Brace Expansion Bash will expand that to Bash 将其扩展为

ls /tmp/C[A-Z][A-Z]/*.log /tmp/C[A-Z][A-Z][A-Z]/*.log

And then do Filename Expansion然后文件名扩展

in perl在 perl

$cmd=`/bin/ls /tmp/C{[A-Z][A-Z],[A-Z][A-Z][A-Z]}/*.log`;

The backticks will call out to /bin/sh not bash , so the brace expansion will not happen反引号将调用/bin/sh而不是bash ,因此不会发生大括号扩展

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM