如何将 bash 中的 While/Case 语句转换为 perl
这是 bash 中的循环:
while [ $# -ge 1 ]; do case $1 in -a) shift NUM_AGENTS=$1 ;; -h) shift HOST_NAME=$1 ;; -t) shift TIME_STAGGER=$1 ;; -un) shift USER_NAME=$1 ;; -pw) shift USER_PASS=$1 ;; -p) shift TARGET_PAGE=$1 ;; -s) shift COMMON_SID=$1 ;; esac shift done
How can I Convert this in perl so that the argument will populate the values in the command line
php loadAgent_curl.php $NUM_AGENTS $HOST_NAME $procStartTime $i $TARGET_PAGE $reqlogfile $resplogfile $USER_NAME $USER_PASS $execDelay $COMMON_SID &
-------- 附加到问题:
这当然有帮助,我真的很感激,有没有什么如何在 getOptions 之外访问这些参数?这是 bash 脚本的其余部分:
my $i="0"; my $startTime=date +%s
; startTime=$[$startTime+$NUM_AGENTS+10] my $PWD=pwd
; my $logdir="\$PWD/load-logs"; system(mkdir $logdir/$startTime); my $reqlogfile="$logdir/$startTime/req.log"; my $resplogfile="$logdir/$startTime/resp.log";打印“\n”; 打印“##################\n”; 打印“启动请求\n”; 打印“主机名:\$HOST_NAME\n”; print " 目标页 : \$TARGET_PAGE\n "; print " # 代理: \$NUM_AGENTS\n "; print " EXECUTION TIME : \$startTime (在 0 到 \$TIME_STAGGER 秒之间随机交错)\n "; 打印“请求日志文件:$reqlogfile\n”; print " RESP 日志文件: $resplogfile\n "; 打印“##################\n”; 打印“\n”; # # HighestStart=$startTime
$startTime += $ARGV[0] + 5; 我的 $dTime = 当地时间( $startTime ); print "\n##################\n启动请求\n 计数:$ARGV[0]\n 延迟: | 1 \n 脚本将在以下时间触发: $dTime\n##################\n\n"; 而 ( $ARGV[0] > $i ) { $i++; 系统(“php avtestTimed.php $ARGV[0] $ARGV[2] $startTime”); print "运行系统('php avtestTimed.php $ARGV[0] $ARGV[2] $startTime'); \n"; 睡觉1; }
# # 而 [ $NUM_AGENTS -gt "$i" ] 做 我=$[$i+1] execDelay=$((随机% $TIME_STAGGER))"."$((随机% 100)) 过程开始时间=$[$开始时间] procStartTime=$[$startTime+$execDelay] 如果 [ $procStartTime -gt $highestStart ] 然后 最高开始=$procStartTime 菲 echo "状态: 排队请求 $i 延迟 $execDelay 秒"
echo " '--> 命令: php loadAgent_curl.php $NUM_AGENTS $HOST_NAME $procStartTime $i $TARGET_PAGE $reqlogfile $resplogfile $USER_NAME $USER_PASS $execDelay $ COMMON_SID"
php loadAgent_curl.php $NUM_AGENTS $HOST_NAME $procStartTime $i $TARGET_PAGE $reqlogfile $resplogfile $USER_NAME $USER_PASS $execDelay $COMMON_SID & 睡觉 1 完成
回显“状态:等待排队的请求准备好” while [
日期 +%s
-lt $startTime ] 做 睡觉 1 完毕 # echo“状态:等待发出最后一个请求” while [date +%s
-lt $highestStart ] 做 睡觉 1 完毕 # echo“状态:上次响应已发布” # echo“状态:等待创建响应日志文件” 尽管 [ ! -e“$resplogfile”] 做 睡觉 1 完毕 # while [wc -l "$resplogfile"| awk '{print $1'}
-lt $NUM_AGENTS ] 做 #echo "(wc -l "$resplogfile"| awk '{print $1'}
记录了 $NUM_AGENTS 响应)" 睡觉 1 完毕 echo "状态:已完成"while true;做 read -p "你想查看请求日志吗?[y/n]" yn 案例 $yn 在 [Yy]* ) cat $reqlogfile;休息;; [Nn]* ) 退出;; * ) echo "请回答是或否。";; 埃萨克 真实时已完成
;做 read -p "你想查看响应日志吗?[y/n]" yn 案例 $yn 在 [Yy]* ) cat $resplogfile;休息;; [Nn]* ) 退出;; * ) echo "请回答是或否。";; 埃萨克 完毕
Here is the loop in bash:
while [ $# -ge 1 ]; do case $1 in -a) shift NUM_AGENTS=$1 ;; -h) shift HOST_NAME=$1 ;; -t) shift TIME_STAGGER=$1 ;; -un) shift USER_NAME=$1 ;; -pw) shift USER_PASS=$1 ;; -p) shift TARGET_PAGE=$1 ;; -s) shift COMMON_SID=$1 ;; esac shift done
How can i convert this in perl so that the argument would populate the values in the command line
php loadAgent_curl.php $NUM_AGENTS $HOST_NAME $procStartTime $i $TARGET_PAGE $reqlogfile $resplogfile $USER_NAME $USER_PASS $execDelay $COMMON_SID &
------- appended to question:
this certainly helps, and i really appreciate it, is there any way to access these parameters outside the getOptions ? here is rest of the bash script:
my $i="0"; my $startTime=date +%s
; startTime=$[$startTime+$NUM_AGENTS+10] my $PWD=pwd
; my $logdir="\$PWD/load-logs"; system(mkdir $logdir/$startTime); my $reqlogfile="$logdir/$startTime/req.log"; my $resplogfile="$logdir/$startTime/resp.log";print "\n"; print "##################\n"; print "LAUNCHING REQUESTS\n"; print " HOST NAME : \$HOST_NAME\n "; print " TARGET PAGE : \$TARGET_PAGE\n "; print " # AGENTS : \$NUM_AGENTS\n "; print " EXECUTION TIME : \$startTime (with random stagger between 0 and \$TIME_STAGGER seconds)\n "; print " REQ LOG FILE : $reqlogfile\n "; print " RESP LOG FILE : $resplogfile\n "; print "##################\n"; print "\n"; # # highestStart=$startTime
$startTime += $ARGV[0] + 5; my $dTime = localtime( $startTime ); print "\n##################\nLAUNCHING REQUESTS\n COUNT: $ARGV[0]\n DELAY: | 1 \n The scripts will fire at : $dTime\n##################\n\n"; while ( $ARGV[0] > $i ) { $i++; system("php avtestTimed.php $ARGV[0] $ARGV[2] $startTime"); print "RUN system('php avtestTimed.php $ARGV[0] $ARGV[2] $startTime'); \n"; sleep 1; }
# # while [ $NUM_AGENTS -gt "$i" ] do i=$[$i+1] execDelay=$((RANDOM % $TIME_STAGGER))"."$((RANDOM % 100)) procStartTime=$[$startTime] procStartTime=$[$startTime+$execDelay] if [ $procStartTime -gt $highestStart ] then highestStart=$procStartTime fi echo "STATUS: Queueing request $i with a delay of $execDelay seconds"
echo " '--> COMMAND: php loadAgent_curl.php $NUM_AGENTS $HOST_NAME $procStartTime $i $TARGET_PAGE $reqlogfile $resplogfile $USER_NAME $USER_PASS $execDelay $COMMON_SID"
php loadAgent_curl.php $NUM_AGENTS $HOST_NAME $procStartTime $i $TARGET_PAGE $reqlogfile $resplogfile $USER_NAME $USER_PASS $execDelay $COMMON_SID & sleep 1 done
echo "STATUS: Waiting for queued requests to be ready" while [
date +%s
-lt $startTime ] do sleep 1 done # echo "STATUS: Waiting for last request to issue" while [date +%s
-lt $highestStart ] do sleep 1 done # echo "STATUS: Last response issued" # echo "STATUS: Waiting for response log file to be created" while [ ! -e "$resplogfile" ] do sleep 1 done # while [wc -l "$resplogfile"| awk '{print $1'}
-lt $NUM_AGENTS ] do #echo "(wc -l "$resplogfile"| awk '{print $1'}
of $NUM_AGENTS responses recorded)" sleep 1 done echo "STATUS: FINISHED"while true; do read -p "Do you wish to view the request log? [y/n]" yn case $yn in [Yy]* ) cat $reqlogfile; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done
while true; do read -p "Do you wish to view the response log? [y/n]" yn case $yn in [Yy]* ) cat $resplogfile; break;; [Nn]* ) exit;; * ) echo "Please answer yes or no.";; esac done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Getopt::Long
库是处理命令行选项的标准 Perl 方法。像这样的东西会起作用。未经测试 - 买者自负!
请注意,由于您的 PHP 参数是命令行选项和一些未识别变量之间的混合,因此我设计了第一个示例,以便所有可能的选项应存储在
%args
哈希中(例如,您的程序应该使用$args{procStartTime}
而不是$procStartTime
)。这使我能够将其变得非常简短和通用。如果这很难阅读/理解,我还有第二个例子,它更简单但不太通用
第二个不太高级但更直接的选项是:
Getopt::Long
library is a standard Perl way to process command line options.Something like this will work. Not tested - caveat emptor!
Please note that since your PHP parameters are a mix between command line options AND some unidentified variables, I have designed the first example so that ALL the possible options should be stored in
%args
hash (e.g. your program should use$args{procStartTime}
instead of$procStartTime
). This allowed me to make it very short and generic.If this is hard to read/understand, I also have a second example that's more straightforward but less generic
A second, less advanced but more direct option is: