Sponsored Content
Top Forums Shell Programming and Scripting Valid input compared with input Post 302437519 by ckozler on Thursday 15th of July 2010 09:29:24 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
libgii(7)								GGI								 libgii(7)

NAME
libgii - A flexible library for input handling DESCRIPTION
LibGII is an input library developed by the GGI Project. Its design philosophy is similar to LibGGI, which deals with graphics output. LibGII is based on the concept of input streams, which virtualize access to the underlying input drivers. Events from various input devices are abstracted into easy-to-use structures. LibGII also allows the application to join streams together, receiving input from an arbitrary combination of devices. LibGII is a separate component from LibGGI, although LibGGI depends on LibGII for input purposes. (LibGGI's input functions are simply wrappers for LibGII functions.) ENVIRONMENT VARIABLES
The following outlines the environment variables, intended for the user, which affect the behaviour of LibGII: GII_INPUT The default input source(s) and/or filter(s) to use when giiOpen() is called with a NULL argument. The format is: input:inputargs. Multiple inputs can be specified by this notation: (i1):(i2) .... Note: This setting does not affect LibGGI visuals automatically opening inputs. Use the GGI_INPUTx variable instead. GII_DEBUG The debug level for LibGII: o 0 or unset : debug output is off; debugging is off o 255 : all debug output is on You may also bitwise 'or' any of the following together: o 2 : debug core o 32 : misc debugging output o 64 : debug dynamic library handling o 128 : debug event handling GII_DEBUGSYNC Turn on synchronous debug output, flushing the output buffers before returning from DPRINT calls. GII_CONFDIR Override compiled-in path to global config files (Win32 only, but not Cygwin). Other environment variables specific to filters and inputs are documented in the corresponding manual page. SEE ALSO
libgii.conf(5), filter-keytrans(7), filter-mouse(7), filter-save(7), filter-tcp(7), input-directx(7), input-linux-kbd(7), input-mouse(7), input-file(7), input-linux-mouse(7), input-tcp(7), input-linux-evdev(7), input-lk201(7). libgii-1.0.x 2006-12-30 libgii(7)
All times are GMT -4. The time now is 09:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy