Perl : accept multiple user entered command line arguemnts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : accept multiple user entered command line arguemnts
# 1  
Old 09-12-2013
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.


Code:
#!/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
# 2  
Old 09-12-2013
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.
# 3  
Old 09-12-2013
Set a flag at the start, use a while loop based on it, unset it once you are happy all is well.
Code:
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.
This User Gave Thanks to Smiling Dragon For This Post:
# 4  
Old 09-16-2013
Thank you Smiling et al ... redo works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Homework & Coursework Questions

Make a file accept only two arguments from the command line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) The script is executed in the Korn shell. 2) Name the shell script file is asg6s. 3) The asg6s file is... (7 Replies)
Discussion started by: ProgMan2015
7 Replies

3. Programming

Make a file accept only two arguments from the command line

DELETED (2 Replies)
Discussion started by: ProgMan2015
2 Replies

4. Shell Programming and Scripting

How to accept command line argument as character or text if number is entered?

Hello Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex (6 Replies)
Discussion started by: aadityapatel198
6 Replies

5. Shell Programming and Scripting

How to loop through array who's name is entered in command line?

Say I have a ksh program called test.ksh which has several defined arrays inside it such as array1,array2,array3...These arrays contain strings. I also have a method in the program: for x in $1 do ....(#do something) done So, when the user enteres: ./test.ksh array1, I want to... (3 Replies)
Discussion started by: mrskittles99
3 Replies

6. Programming

How to accept multiple lines input from User in C?

Hi I want to accept multiple lines input with spaces from User and i have a working code like this. char sRes; char sReq; printf("Please enter request:"); scanf("%",sReq); /* Accept the input from user */ printf("\nPlease enter response:"); scanf("%",sRes); but the... (4 Replies)
Discussion started by: AAKhan
4 Replies

7. Shell Programming and Scripting

SQL PLUS Command 'ACCEPT' is not waiting for user input with sh shell script

Dear All, The sqlplus 'Accept' command is not waiting for user input when I include the command within a shell script. Note: The 'Accept' command is working fine if I execute it in a SQLPLUS Prompt. Please fins the below sample script which i tried. SCRIPT: -------- #!... (4 Replies)
Discussion started by: little_wonder
4 Replies

8. UNIX for Dummies Questions & Answers

Append 0 for single digit entered from command line

I have a script like this-- #!/bin/ksh echo "To pad a 0 before digits from 1-9" for i in $* do echo $i | sed 's//'0'/g' done I run this script as ksh name 1 2 23 34 The output should be 01 02 23 34 Help me in modifying this script. Thanks Namish (2 Replies)
Discussion started by: namishtiwari
2 Replies

9. Solaris

Removing user from multiple groups via command line

Want to know if any, a command line parameter(s) of removing a user from multiple groups without using any ineractive application? (1 Reply)
Discussion started by: jquizon62
1 Replies

10. Shell Programming and Scripting

getting rid of $ when entered at the command line

Hi everyone, Can someone possibly help me with this problem I am having please. I wrote a Korn shell script to manipulate currency amounts in a way that a person could use this script to determine the minimum number of coins required to make a certain amount. for example when entered on the... (2 Replies)
Discussion started by: bashirpopal
2 Replies
Login or Register to Ask a Question