1.使用一个参数读取shell传递来的多个参数:
目录下:
a.log
b.log
c.log
dota.pl
dota.pl的内容如下:
#! /usr/bin/perl
use Getopt::Std;
use warnings;
use strict;
sub read_from_sh($) {
my $file = shift;
my @files = ();
open F, $file or die "Could not open $file: $!";
while (<F>) {
next if /^\s*$/;
push @files, $_;
}
close F or die "Could not close $file: $!";
return @files;
}
my @files;
my %opts = ();
getopts("s:", \%opts);
if ($opts{'s'}) {
@files = read_from_sh($opts{'s'});
}else {
@files = @ARGV;
}
for my $file (@files) {
print "file: $file\n";
}
运行的shell如下:
find -name '*log' | /usr/bin/perl dota.pl -s -
结果如下:
file:a.log
file:b.log
file:c.log
或者直接塞给perl:/usr/bin/perl dota.pl a.log b.log
结果同上
2.向perl程序传递多个参数:
#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use vars qw($opt_a $opt_b $opt_c);
getopts('a:b:c:');
print "\$opt_a =>; $opt_a\n" if $opt_a;
print "\$opt_b =>; $opt_b\n" if $opt_b;
print "\$opt_c =>; $opt_c\n" if $opt_c;
/usr/bin/perl p1.pl -a aa -b bb -c cc
目录下:
a.log
b.log
c.log
dota.pl
dota.pl的内容如下:
#! /usr/bin/perl
use Getopt::Std;
use warnings;
use strict;
sub read_from_sh($) {
my $file = shift;
my @files = ();
open F, $file or die "Could not open $file: $!";
while (<F>) {
next if /^\s*$/;
push @files, $_;
}
close F or die "Could not close $file: $!";
return @files;
}
my @files;
my %opts = ();
getopts("s:", \%opts);
if ($opts{'s'}) {
@files = read_from_sh($opts{'s'});
}else {
@files = @ARGV;
}
for my $file (@files) {
print "file: $file\n";
}
运行的shell如下:
find -name '*log' | /usr/bin/perl dota.pl -s -
结果如下:
file:a.log
file:b.log
file:c.log
或者直接塞给perl:/usr/bin/perl dota.pl a.log b.log
结果同上
2.向perl程序传递多个参数:
#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use vars qw($opt_a $opt_b $opt_c);
getopts('a:b:c:');
print "\$opt_a =>; $opt_a\n" if $opt_a;
print "\$opt_b =>; $opt_b\n" if $opt_b;
print "\$opt_c =>; $opt_c\n" if $opt_c;
/usr/bin/perl p1.pl -a aa -b bb -c cc