Pregunta

Aquí es el bucle en 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

¿Cómo puedo convertir esto en Perl para que el argumento sería rellenar los valores en la línea de comandos

php loadAgent_curl.php $NUM_AGENTS $HOST_NAME $procStartTime $i $TARGET_PAGE $reqlogfile $resplogfile $USER_NAME $USER_PASS $execDelay $COMMON_SID &

------- anexa a la pregunta:

Esto sin duda ayuda, y se lo agradezco mucho, ¿hay alguna manera de acceder a estos parámetros fuera de los getOptions? aquí es el descanso de la escritura del golpe:

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

¿Fue útil?

Solución

biblioteca Getopt::Long es una forma estándar de Perl para las opciones de línea de comandos de proceso.

Algo como esto va a funcionar. No se ha probado - caveat.emptor

Tenga en cuenta que, dado que los parámetros de PHP son una mezcla entre las opciones de línea de comandos y algunas variables no identificadas, he diseñado el primer ejemplo de manera que todas las opciones posibles se deben almacenar en picadillo %args (por ejemplo, su programa debe utilizar $args{procStartTime} en lugar de $procStartTime ). Esto me permitió hacer que sea muy corto y genérico. Si esto es difícil de leer / entender, también tengo un segundo ejemplo que es más sencillo, pero menos genérico

use Getopt::Long;
my @php_arg_order = qw(a h procStartTime i p reqlogfile 
                       resplogfile un pw execDelay s);

my %args = map {$_ => ""} @php_arg_order;

$args{procStartTime} = "something";
$args{reqlogfile} = "a.log"; 
# More defaults for variables NOT passed in via command line. 
# Populate them all in %args as above.

# Now load actual command line parameters.
GetOptions(\%args, map { "$_=s" } @php_arg_order) or die "Unknown parameter!\n";

system(join(" ", 
            "php", "loadAgent_curl.php",map { $args{$_} } @php_arg_order}, "&"));

Una segunda opción menos avanzado, pero es más directo:

use Getopt::Long;

my %args = ();

# Now load actual command line parameters.
GetOptions(\%args, 
   "NUM_AGENTS|a=s"
  ,"HOST_NAME|h=s"
  ,"USER_NAME|un=s"
  # ... the rest of options
  # The "XXX|x" notaion allows using alias "-x" parameter
  # but stores in $args{XXX} instead for better readability
) or die "Unknown parameter!\n";

system("php loadAgent_curl.php $args{NUM_AGENTS} $args{HOST_NAME} $procStartTime $i $args{TARGET_PAGE} $reqlogfile $resplogfile $args{USER_NAME} $args{USER_PASS} $execDelay $args{COMMON_SID} &");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top