在 Linux 上使用 Getopt::Long 驱动系统命令
我正在尝试使用以下 perl 驱动程序执行带参数的 php 脚本:
#!/opt/local/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Cwd;
my %args = ();
GetOptions(
\%args, "NUM_AGENTS|a=s",
"HOST_NAME|h=s", "TIME_STAGGER|t=s",
"USER_NAME|un=s", "USER_PASS|pw=s",
"TARGET_PAGE|p=s"
) or die "Unknown parameter!\n";
my $i = 0;
my $startTime = time;
my $pwd = getcwd();
my $logdir = "$pwd/load-logs";
mkdir $logdir
or die "Cannot mkdir $logdir: $!"
unless -d $logdir;
chmod 0755, $logdir or die "Cannot chmod 0755 $logdir: $!";
my $startTimeTemp = $args{NUM_AGENTS} + $startTime;
my $startTime2 = $startTimeTemp + 10;
mkdir( "$logdir/$startTime2", 0777 )
or die "Cannot mkdir $logdir/$startTime2: $!"
unless -d "$logdir/$startTime2";
my $random_number = rand() * 10;
my $execDelay =
( $random_number % $args{TIME_STAGGER} ) * ( ( $random_number % 100 ) );
my $procStartTime = $startTime2 + $execDelay;
my $reqlogfile = "$logdir/$startTime2/req.log";
my $resplogfile = "$logdir/$startTime2/resp.log";
print "NUM_AGENTS: " . "$args{NUM_AGENTS}\n";
print "HOST_NAME: " . "$args{HOST_NAME}\n";
print "procStartTime: " . "$procStartTime\n";
print "i: " . "$i\n";
print "TARGET_PAGE: " . "$args{TARGET_PAGE}\n";
print "resplogfile: " . "$resplogfile\n";
print "USER_NAME: " . "$args{USER_NAME}\n";
print "USER_PASS: " . "$args{USER_PASS}\n";
print "execDelay: " . "$execDelay\n";
print "COMMON_SID: " . "$args{COMMON_SID}\n";
while ( $args{NUM_AGENTS} > $i ) {
$i++;
print "count: " . "$i\n";
my $argString =
"'$args{NUM_AGENTS}' '$args{HOST_NAME}' '$procStartTime' '$i' '$args{TARGET_PAGE}' 'resplogfile' '$reqlogfile' '$args{USER_NAME}' '$args{USER_PASS}' '$execDelay' '$args{COMMON_SID}'";
system("php loadAgent_curl.php $argString") == 0 or die "failed to execute: $!";
sleep 1;
#system("ls");
}
但似乎有问题:
system("php loadAgent_curl.php $argString")
因为 ls 系统命令运行良好,但带参数的 php 命令不运行
此 perl 的命令行参数脚本可以是:
-a 10 -h ktest.test.net -t 5 -un admin -pw adminpassword -p "acViewer/index.html?StartDate=20090926040000&EndDate=20111220235959"
I am trying to execute a php script with arguments, using the following perl driver:
#!/opt/local/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Cwd;
my %args = ();
GetOptions(
\%args, "NUM_AGENTS|a=s",
"HOST_NAME|h=s", "TIME_STAGGER|t=s",
"USER_NAME|un=s", "USER_PASS|pw=s",
"TARGET_PAGE|p=s"
) or die "Unknown parameter!\n";
my $i = 0;
my $startTime = time;
my $pwd = getcwd();
my $logdir = "$pwd/load-logs";
mkdir $logdir
or die "Cannot mkdir $logdir: $!"
unless -d $logdir;
chmod 0755, $logdir or die "Cannot chmod 0755 $logdir: $!";
my $startTimeTemp = $args{NUM_AGENTS} + $startTime;
my $startTime2 = $startTimeTemp + 10;
mkdir( "$logdir/$startTime2", 0777 )
or die "Cannot mkdir $logdir/$startTime2: $!"
unless -d "$logdir/$startTime2";
my $random_number = rand() * 10;
my $execDelay =
( $random_number % $args{TIME_STAGGER} ) * ( ( $random_number % 100 ) );
my $procStartTime = $startTime2 + $execDelay;
my $reqlogfile = "$logdir/$startTime2/req.log";
my $resplogfile = "$logdir/$startTime2/resp.log";
print "NUM_AGENTS: " . "$args{NUM_AGENTS}\n";
print "HOST_NAME: " . "$args{HOST_NAME}\n";
print "procStartTime: " . "$procStartTime\n";
print "i: " . "$i\n";
print "TARGET_PAGE: " . "$args{TARGET_PAGE}\n";
print "resplogfile: " . "$resplogfile\n";
print "USER_NAME: " . "$args{USER_NAME}\n";
print "USER_PASS: " . "$args{USER_PASS}\n";
print "execDelay: " . "$execDelay\n";
print "COMMON_SID: " . "$args{COMMON_SID}\n";
while ( $args{NUM_AGENTS} > $i ) {
$i++;
print "count: " . "$i\n";
my $argString =
"'$args{NUM_AGENTS}' '$args{HOST_NAME}' '$procStartTime' '$i' '$args{TARGET_PAGE}' 'resplogfile' '$reqlogfile' '$args{USER_NAME}' '$args{USER_PASS}' '$execDelay' '$args{COMMON_SID}'";
system("php loadAgent_curl.php $argString") == 0 or die "failed to execute: $!";
sleep 1;
#system("ls");
}
but it seems, that something is wrong with :
system("php loadAgent_curl.php $argString")
since the ls system commands runs fine, but the php command with arguments does not
The Command Line arguments ot this perl script can be:
-a 10 -h ktest.test.net -t 5 -un admin -pw adminpassword -p "acViewer/index.html?StartDate=20090926040000&EndDate=20111220235959"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与大多数其他 Perl 命令不同,
system
在“成功”时返回 0,在“失败”时返回非零。因此,典型的习惯用法是代替
更新:OP 确实正确地完成了这部分 -
system(...)==0 or die ...
也是一个完美的对system
命令进行错误检查的好方法。您传递给
system
命令的确切命令中也可能有一些时髦的引用。对于这样的任务,通常最好绕过 shell 并使用system
的 LIST 形式将命令直接传递给操作系统。也许是这样的:(并确保
php
位于您的$PATH
中 [或指定php
的完整路径] 和loadAgent_curl .php
在当前目录中)。Unlike most other Perl commands,
system
returns 0 on "success" and non-zero on "failure". So the typical idiom isinstead of
Update: the OP did get this part right --
system(...)==0 or die ...
is also a perfectly fine way to do error checking on thesystem
command.There also might be some funky quoting in the exact command you are passing to the
system
command. For a task like this, it is often best to bypass the shell and use the LIST form ofsystem
to pass the command directly to the OS. Maybe something like:(and also make sure
php
is in your$PATH
[or specify the complete path tophp
] andloadAgent_curl.php
is in the current directory).您没有提到您收到的错误消息的类型。是类似
找不到“php”
之类的东西吗?您可能在引用时遇到问题。这里有一些建议:
qq()
而不是引号。这会让事情变得更干净一些。$command
的变量中,您可以将其打印出来,以防出现错误。这可能会帮助您了解您可能在哪里遇到问题。system
时设置一个名为$error
的变量,然后检查该变量。它比在失败和成功时必须执行的和/或
内容的向后风格要清晰得多,并且更容易维护。示例:
至少通过这种方式,您可以看到哪个命令失败,并更好地了解它可能未执行的原因。
You didn't mention the type of error message you're getting. Was it something like
Cannot find "php"
or something else.You might be having trouble with quotes. Here's a few recommendations:
qq()
instead of quotation marks. That'll make things a bit cleaner.$command
which you can print out in case you have errors. That might help you understand where you maybe having a problem.$error
when executingsystem
, and then check that variable. It's a lot clearer than the backwards style ofand/or
stuff that you have to do on failure and success, and it's a lot easier to maintain.Example:
At least this way, you can see what command failed, and get a better idea why it might not have executed.
尝试 PHP - GetOptionKit:
http://c9s.blogspot.com/2011/11/ php-getoptionkit.html
概要
Try PHP - GetOptionKit:
http://c9s.blogspot.com/2011/11/php-getoptionkit.html
synopsis