How can i convert the following bash script into a perl script -
#!/bin/bash i="0" echo "" echo "##################" echo "launching requests" echo " count: $2 " echo " delay: $3 " echo " sessid: $1" echo "##################" echo "" while [ $2 -gt "$i" ] i=$[$i+1] php avtest.php $1 $4 & echo "executing request $i" sleep $3 done
here better/modified script in bash
#!/bin/bash i="0" #starttime=`date +%s` starttime=$(date -u +%s) starttime=$[$starttime+$1+5] #starttime=$($starttime+$1+5) dtime=`date -d @$starttime` echo "" echo "##################" echo "launching requests" echo " count: $1 " echo " delay: 1 " #echo " execution: $starttime " echo " scripts fire @ : $dtime " echo "##################" echo "" while [ $1 -gt "$i" ] i=$[$i+1] php avtesttimed.php $1 $3 $starttime & echo "queueing request $i" sleep 1 done
here's direct translation
#!/usr/bin/env perl use strict; use warnings; print <<here; ################## launching requests count: $argv[1] delay: $argv[2] sessid: $argv[0] ################## here $i = 0; while($argv[1] > $i){ $i += 1; system("php avtest.php $argv[0] $argv[3] &"); print "executing request $i\n"; sleep $argv[2]; }
but make more sense read command line parameters variables named after they're , not rely on remembering argument ordering.
a brief errata in conversion:
i use here string represent multiline text. have put in multiple print
statements more closely mimic bash version
in bash arguments accessed numbered variables, starting $1 , going up. in perl argument list represented array @argv, numbered starting @ 0 (like arrays in languages). in both bash , perl name of script can found in variable $0.
in perl arrays written @arrayname
when refering entire array, use $arrayname[index] when accessing array members. perl $list[0]
bash ${list[0]}
, perl @list
bash ${list[@]}
.
in perl variables declared my
keyword; equivalent in bash declare
.
i've used system
function spawning background processes. argument can command line might use in bash.
unlike echo
, print
requires told if there should newline @ end of line. recent versions of perl say
function exists append newline you.
the perl sleep
function pretty self-explanatory.
edit: due typo $i
in print statement had been represented $ni
leading runtime errors. has been corrected.
Comments
Post a Comment