Perl : accept multiple user entered command line arguemnts

I am trying to create a script that will accept multi input from the user (really just me), then execute those command on a remote device.

My question is if the I enter "No" at the confirmation point "Are these statements correct y or n ?", what is the best way to go back and start over ? I have been trying different things with while and until flags but nothing is working.


#!/usr/bin/perl
use warnings;
use strict;
my @cmdlist;
my $cmd;
system("clear");
print "\nEnter one command per line then press enter.\n";
print "when you are complete entering data, press enter.\n"; 
 while (<>) {
  last if ($_ =~ /^\s*$/); # Exit if just spaces or an enter
  print "You typed: $_";
  push @cmdlist, $_;
 }
#
system("clear");
print "The following is what will be sent to the remote device.\n" ;
 foreach (@cmdlist) {
  do
   print "$_";
  }
#
print "Are these statements correct y or n ?\n" ;
my $ans=<>;
 if ($ans =~ m/n/i) {
  print "No\n"; 
 }

Thanks

You could try wrapping your code in an infinite while loop and then when the answer is "No", use the redo function to return to the beginning of the loop.

Set a flag at the start, use a while loop based on it, unset it once you are happy all is well.

my $redo=1;
while ($redo) {

  <ask your questions>

  print "Are these statements correct y or n ?\n";
  my $ans=<>;
  if ($ans =~ m/n/i) {
    print "No\n"; 
  } else {
    $redo=0;
  }
}

You don't need the 'm' in 'm/n/i' by the way, because you are doing a readline (<>) you're parsing a single line by definition.

1 Like

Thank you Smiling et al ... redo works.