唔。如果您使用perl,那麼您並不需要致電find(1)
!如果您使用File::Find模塊,則可以在沒有外部呼叫的情況下擁有更好的find
。試試這樣:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my $dir = "/opt/exploit/dev/florian/scan-allied/working-dir/";
my $addresse ="751471" ;
my $ip = "126.108.216.254";
my $re = "shswitch_${addresse}_${ip}_\d+";
sub wanted {
/^$re$/ and -f $_ and print "$_\n";
}
find \&wanted, $dir;
這將打印所有匹配的文件。
您可以使用find2perl
實用工具將完整的find
命令行轉換爲wanted
函數!
對於
find2perl /opt/exploit/dev/florian/scan-allied/working-dir -type f -name \"shswitch_751471_126.108.216.254_${ip}_*\"
下面的代碼給出:
#! /usr/bin/perl -w
eval 'exec /usr/bin/perl -S $0 ${1+"[email protected]"}'
if 0; #$running_under_some_shell
use strict;
use File::Find();
# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.
# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
sub wanted;
# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/opt/exploit/dev/florian/scan-allied/working-dir');
exit;
sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
-f _ &&
/^"shswitch_751471_126\.108\.216\.254__.*"\z/s
&& print("$name\n");
}
你忘了匹配 「shswitch」。在前面添加一個*,記住這不是*正則表達式匹配,而是一個glob,這是非常不同的。 –
始終使用'use strict;使用警告;'! – ikegami