在 Linux 上使用 Getopt::Long 驱动系统命令

发布于 2024-10-10 07:22:46 字数 2203 浏览 8 评论 0原文

我正在尝试使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绮筵 2024-10-17 07:22:46

与大多数其他 Perl 命令不同,system 在“成功”时返回 0,在“失败”时返回非零。因此,典型的习惯用法是

system $command and die ...

代替

system $command or die ...

更新:OP 确实正确地完成了这部分 - system(...)==0 or die ... 也是一个完美的对 system 命令进行错误检查的好方法。


您传递给 system 命令的确切命令中也可能有一些时髦的引用。对于这样的任务,通常最好绕过 shell 并使用 system 的 LIST 形式将命令直接传递给操作系统。也许是这样的:(

my @argList = ($args{NUM_AGENTS}, $args{HOST_NAME}, $procStartTime, $i,
               $args{TARGET_PAGE}, 'resplogfile', $reqlogfile, $args{USER_NAME},
               $execDelay, $args{COMMON_SID});
system("php", "loadAgent_curl.php", @argList) and die "failed to execute: $!";

并确保 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 is

system $command and die ...

instead of

system $command or die ...

Update: the OP did get this part right -- system(...)==0 or die ... is also a perfectly fine way to do error checking on the system 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 of system to pass the command directly to the OS. Maybe something like:

my @argList = ($args{NUM_AGENTS}, $args{HOST_NAME}, $procStartTime, $i,
               $args{TARGET_PAGE}, 'resplogfile', $reqlogfile, $args{USER_NAME},
               $execDelay, $args{COMMON_SID});
system("php", "loadAgent_curl.php", @argList) and die "failed to execute: $!";

(and also make sure php is in your $PATH [or specify the complete path to php] and loadAgent_curl.php is in the current directory).

狼亦尘 2024-10-17 07:22:46

您没有提到您收到的错误消息的类型。是类似找不到“php”之类的东西吗?

您可能在引用时遇到问题。这里有一些建议:

  • 使用 qq() 而不是引号。这会让事情变得更干净一些。
  • 将命令构建到名为 $command 的变量中,您可以将其打印出来,以防出现错误。这可能会帮助您了解您可能在哪里遇到问题。
  • 在执行system时设置一个名为$error的变量,然后检查该变量。它比在失败和成功时必须执行的 和/或 内容的向后风格要清晰得多,并且更容易维护。

示例:

my $argString = qq("$args{NUM_AGENTS}" "$args{HOST_NAME}" ) 
    . qq( "$procStartTime" "$i" "$args{TARGET_PAGE}" "resplogfile" )
    . qq("$reqlogfile" "$args{USER_NAME}" "$args{USER_PASS}" )
    . qq("$execDelay" "$args{COMMON_SID}");

my $command = qq(php loadAgent_curl.php $argString);

my $error = system qq($command);
if ($error) {
    die qq(ERROR: Failed to execute "$command"\n\n$!);
}

至少通过这种方式,您可以看到哪个命令失败,并更好地了解它可能未执行的原因。

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:

  • Use qq() instead of quotation marks. That'll make things a bit cleaner.
  • Build the command into a variable called $command which you can print out in case you have errors. That might help you understand where you maybe having a problem.
  • Set a variable called $error when executing system, and then check that variable. It's a lot clearer than the backwards style of and/or stuff that you have to do on failure and success, and it's a lot easier to maintain.

Example:

my $argString = qq("$args{NUM_AGENTS}" "$args{HOST_NAME}" ) 
    . qq( "$procStartTime" "$i" "$args{TARGET_PAGE}" "resplogfile" )
    . qq("$reqlogfile" "$args{USER_NAME}" "$args{USER_PASS}" )
    . qq("$execDelay" "$args{COMMON_SID}");

my $command = qq(php loadAgent_curl.php $argString);

my $error = system qq($command);
if ($error) {
    die qq(ERROR: Failed to execute "$command"\n\n$!);
}

At least this way, you can see what command failed, and get a better idea why it might not have executed.

贩梦商人 2024-10-17 07:22:46

尝试 PHP - GetOptionKit:

http://c9s.blogspot.com/2011/11/ php-getoptionkit.html

概要

use GetOptionKit\GetOptionKit;

$getopt = new GetOptionKit;
$spec = $getopt->add( 'f|foo:' , 'option require value' );  # returns spec object.

$getopt->add( 'b|bar+' , 'option with multiple value' );
$getopt->add( 'z|zoo?' , 'option with optional value' );

$getopt->add( 'f|foo:=i' , 'option require value, with integer type' );
$getopt->add( 'f|foo:=s' , 'option require value, with string type' );

Try PHP - GetOptionKit:

http://c9s.blogspot.com/2011/11/php-getoptionkit.html

synopsis

use GetOptionKit\GetOptionKit;

$getopt = new GetOptionKit;
$spec = $getopt->add( 'f|foo:' , 'option require value' );  # returns spec object.

$getopt->add( 'b|bar+' , 'option with multiple value' );
$getopt->add( 'z|zoo?' , 'option with optional value' );

$getopt->add( 'f|foo:=i' , 'option require value, with integer type' );
$getopt->add( 'f|foo:=s' , 'option require value, with string type' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文