On Wed, Aug 1, 2012 at 9:08 AM, Paul.G <medur...@yahoo.com.au> wrote:
> The example below is just a test, I need to be able to insert multiple > values into a command, those values can be either 1, 2 or upto 5. > > Below is closer to the working example, but I will read that document and > to help make a final decision. > > # Check Free PV's > operation_CHECKFREEPVS(); > > $NEWMIRROR = $MIRROR + $numPV; > > if ($numPV ne 0 && $MIRROR le 5) { > # lvextend > print "$numPV $NEWMIRROR $MIRROR\n"; > switch ($numPV) { > case 1 { run("/usr/sbin/lvextend -m $NEWMIRROR -s $sourcelv > $FreePV[0]"); } > case 2 { run("/usr/sbin/lvextend -m $NEWMIRROR -s $sourcelv > $FreePV[0] $FreePV[1]"); } > case 3 { run("/usr/sbin/lvextend -m $NEWMIRROR -s $sourcelv > $FreePV[0] $FreePV[1] $FreePV[2]"); } > case 4 { run("/usr/sbin/lvextend -m $NEWMIRROR -s $sourcelv > $FreePV[0] $FreePV[1] $FreePV[2] $FreePV[3]"); } > case 5 { run("/usr/sbin/lvextend -m $NEWMIRROR -s $sourcelv > $FreePV[0] $FreePV[1] $FreePV[2] $FreePV[3] $FreePV[4]"); > } > } > # lvsync > run("/usr/sbin/lvsync -T $sourcelv"); > logprint "Successful $NEWMIRROR mirrors \t\t synced"; > } > else { > cleanexit (10, "FAIL \t\t No Free PV's Available"); > } > > return 0; > } > > > > ________________________________ > From: John W. Krahn <jwkr...@shaw.ca> > To: Perl Beginners <beginners@perl.org> > Sent: Wednesday, 1 August 2012 4:58 PM > Subject: Re: case statement in perl > > Paul.G wrote: > > Below is an extract from the perl script, the switch/case statement > seemed like a simple solution. > > > > > > > > ##################### Mail Program ##################################### > > > > operation_CHECKFREEPVS(); > > print "$numPV \n"; > > # print "$FreePV[1] $FreePV[0] $numPV\n"; > > if ($numPV ne 0 ) { > > switch ($numPV) { > > case 1 { print "$FreePV[0] \n"; } > > case 2 { print "$FreePV[0] $FreePV[1] \n"; } > > case 3 { print "$FreePV[0] $FreePV[1] $FreePV[2] \n"; } > > } > > } > > Couldn't you just do that like this: > > if ( @FreePV && @FreePV <= 3 ) { > print join( ' ', @FreePV ), "\n"; > } > > > > else { > > print "No PV's available \n"; > > } > > > > John > -- > Any intelligent fool can make things bigger and > more complex... It takes a touch of genius - > and a lot of courage to move in the opposite > direction. -- Albert Einstein > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > Why make life so hard as John suggested you check for the number of arguments and as long as it is less then 5 you simply join them with a space and you are done with a single line... No matter what you do you will always have to check the variables that you are going to push out, right now I could very well feed a parameter that ends in a ; and then do something like *rm -rf /* which could make a serious mess of your system... Personally I would suggest checking the command flags if they are valid, and do not contain any funny characters before even considdering putting this in a system call or equivalent. Regards, Rob