简体   繁体   中英

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

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:

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

if I run ls bash command from script perl:

$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

Can't sort of it.

(not an answer, an explanation)

In your shell

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

That uses bash Brace Expansion . Bash will expand that to

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

And then do Filename Expansion

in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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