Valid input compared with input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Valid input compared with input
# 1  
Old 07-15-2010
Valid input compared with input

Hi everyone,

I cant seem to figure this out for the life of me. Basically, I have two arrays..

Code:
sub init {
        use Getopt::Long;
        use Data::Dumper;
        my @selections = ();
        my @valid_options = ( "vaild1", "valid2" );
        GetOptions( "input=s" => \@selections );
        
        #
        # Loop input and compare against valid options storing invalid options
        #       
}

So as you can see from the above code, I take in input from the user in the command line as --input and want to compare it against valid/supported options.

For example, if they input --input=valid1 --input=valid3 --input=valid2 then it will tell them "valid3 is not a supported option". I have tried embedded loops (for/foreach/etc) and comparing -- as such:

Code:
for my $i( @selections ) { 
        for my $j( @valid_options ) {
                if( $i ne $j ) { print "not valid ( $i = $j)"; exit; }
        }
}

The above is only an example and I have tried many many many different ways but it seems the caveat is that I need to loop the entire array of inputs first and only erroring if no match at all was found-- this does not satisfy me as I would like to tell the user what they have inputted is wrong and which value exactly is wrong.

In short, I want to validate a users input in comparison with what I dictate as valid options-- the current logic I have tried time and time again does not seem to work as expected because it is simply doing a not equal check which occasionally fails because it is still hitting a valid option BUT just because the two strings do not equal each other, it errors out. This is not what I want.


I hope I have made my problem clear.

EDIT: I understand that I could make this very easy for myself by using static checks but I want to make it all dynamic. For instance, if I want to add another valid input, all I do is append it to the array rather than having to go through the code and add a separate block of code (eg: if( $input == "valid3" ) { __...do this...__; } )

For example-- I do not want to have to do this:

Code:
foreach my $input( @selections ) { 
        $input = lc( $input );
        if( $input ne "valid1" and $input ne "valid2" ) { print( "Unsupported ($input)" ); }
        if( $input eq "valid1" ) { print( "Doing action for valid1" ); }
        # etc...
}

Removes the dynamics of it.

Last edited by ckozler; 07-15-2010 at 10:41 AM..
# 2  
Old 07-15-2010
Quote:
Originally Posted by ckozler
...
In short, I want to validate a users input in comparison with what I dictate as valid options-- the current logic I have tried time and time again does not seem to work as expected because it is simply doing a not equal check which occasionally fails because it is still hitting a valid option BUT just because the two strings do not equal each other, it errors out. This is not what I want.
...

You have the @selections array. And you have the @valid_options array. What you want to do is - loop through @selections array, and use grep to check if the current element is present in @valid_options array. If it is, you have a valid option; otherwise not.

Code:
$
$
$ perl -le '@valid_options = qw(valid1 valid2); @selections = qw(valid1 valid2 valid3 valid4);
            for $i (@selections) {print "$i is ", grep(/$i/,@valid_options) ? "Valid!" : "Invalid!"}'
valid1 is Valid!
valid2 is Valid!
valid3 is Invalid!
valid4 is Invalid!
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 07-15-2010
Quote:
Originally Posted by durden_tyler
You have the @selections array. And you have the @valid_options array. What you want to do is - loop through @selections array, and use grep to check if the current element is present in @valid_options array. If it is, you have a valid option; otherwise not.

Code:
$
$
$ perl -le '@valid_options = qw(valid1 valid2); @selections = qw(valid1 valid2 valid3 valid4);
            for $i (@selections) {print "$i is ", grep(/$i/,@valid_options) ? "Valid!" : "Invalid!"}'
valid1 is Valid!
valid2 is Valid!
valid3 is Invalid!
valid4 is Invalid!
$
$

tyler_durden
Perfect! Thank you!

Code:
sub init {
        use Getopt::Long;
        use Data::Dumper;
        my @selections = ();
        my @supported_apps = ( "mysql", "tomcat" );
        GetOptions( "install=s" => \@selections );
        
        for my $i( @selections ) {
                my $supported = grep( /$i/, @supported_apps );
                if( $supported eq 0 ) {
                        _print( "$i is invalid.  Please input a supported application", 1 );
                        _showHelp();
                        exit;   
                }
        }
}

I know the variable names changed but the idea stays the same. I am writing a small perl installer script so I need to keep an organized list of applications I support for installation.


Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

2. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

3. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

6. Shell Programming and Scripting

Validating uppercase/lowercase of user input with perl compared to unix folders

Hi, I need to copy files from a source directory to a destination directory in unix. I'm using the file::copy for the actual copy. The problem is that the source and dest directories are supplied by different users, who might type the name of the directories in various combinations of lower... (6 Replies)
Discussion started by: Furou
6 Replies

7. Shell Programming and Scripting

Problems with valid input

Hello all I am having problems with a part of my script. Basically it asks for the user to enter a new id number for them. The catches are:- * It cannot already be existing (it will check a file) * It has to be four characters * It can only be numbers * It cannot call back into the... (2 Replies)
Discussion started by: ChrisHoogie
2 Replies

8. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

9. Shell Programming and Scripting

How to check for a valid numeric input

Hi Folks, I'm using bash script. I would like to check whether input is a number or not.(Only positive numbers).. if space or non numeric is entered, it should say "invalid input". pls help.. thanks in adv. Br/// Vijay. (1 Reply)
Discussion started by: Vijayakumarpc
1 Replies

10. Shell Programming and Scripting

How to prompt for input & accept input in ONE line

hi, am a new learner to shell programming. i have a script which will prompt for user to key in their name & display their name afterwards. script ===== echo "Pls enter your name:" read name echo "Your name is $name." output ===== Pls enter your name: Bob Your name is Bob. what... (2 Replies)
Discussion started by: newbie168
2 Replies
Login or Register to Ask a Question