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
curl_easy_duphandle(3)						  libcurl Manual					    curl_easy_duphandle(3)

NAME
curl_easy_duphandle - Clone a libcurl session handle SYNOPSIS
#include <curl/curl.h> CURL *curl_easy_duphandle(CURL *handle); DESCRIPTION
This function will return a new curl handle, a duplicate, using all the options previously set in the input curl handle. Both handles can subsequently be used independently and they must both be freed with curl_easy_cleanup(3). All strings that the input handle has been told to point to (as opposed to copy) with previous calls to curl_easy_setopt(3) using char * inputs, will be pointed to by the new handle as well. You must therefore make sure to keep the data around until both handles have been cleaned up. The new handle will not inherit any state information, no connections, no SSL sessions and no cookies. Note that even in multi-threaded programs, this function must be called in a synchronous way, the input handle may not be in use when cloned. RETURN VALUE
If this function returns NULL, something went wrong and no valid handle was returned. SEE ALSO
curl_easy_init(3),curl_easy_cleanup(3),curl_global_init(3) libcurl 7.9 18 September 2001 curl_easy_duphandle(3)
All times are GMT -4. The time now is 12:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy