Sponsored Content
Top Forums Shell Programming and Scripting [Solved] Read and validate input arguments Post 302882872 by AlbertGM on Thursday 9th of January 2014 12:35:00 PM
Old 01-09-2014
[Solved] Read and validate input arguments

Hi,
I need to get input arguments, as well as validate them. This is how I'm reading them:
Code:
 
#!/bin/bash
args="$@" # save arguments to variable 
## Read input arguments, if so 
while [ $# -ge 1 ]; do 
    case $1 in 
        -v | --verbose ) verbose=true;; 
        -z | --gzip ) compression="gz";; 
        -b | --bzip2 ) compression="bz";; 
        -n | --no-compress) compression="none";;
        -h | --help ) showUsage; exit 0 ;; 
        *) echoErr "Invalid option $1. Use -h or --help to show usage"; exit 1 ;; 
    esac
    shift
done

My problem is how validate incompatible arguments. For instance, it's not allowed to call script with both "-z" and "-b", or "-n" and "-z", and so on.
I stored input arguments into a variable called "args" (because shift command unsets them).
Is there a simple way to validate other than a double loop? This is what occurred to me, but is really ugly. Any hint to improve?
Code:
args="$@" # store into args variable
# [...] previous while
argsIterate=($args) # I don't know how to iterate over $args, except convert into an array
for (( i=0;i<${#argsIterate[@]};i++)); do # 1st loop over all arguments
    for (( j=i+1;j<${#argsIterate[@]};j++)); do # 2nd loop over rest arguments
        if ([ "${argsIterate[${i}]}" == "-z" ] && ([ "${argsIterate[${j}]}" == "-b" ] || [ "${argsIterate[${j}]}" == "-n" ])) || \
                ([ "${argsIterate[${i}]}" == "-b" ] && ([ "${argsIterate[${j}]}" == "-z" ] || [ "${argsIterate[${j}]}" == "-n" ])) || \
                ([ "${argsIterate[${i}]}" == "-n" ] && ([ "${argsIterate[${j}]}" == "-z" ] || [ "${argsIterate[${j}]}" == "-b" ])) ; then 
            echo "Incompatible arguments ${argsIterate[${i}]} and ${argsIterate[${j}]}"
            exit -1; 
        fi
    done
done

Thanks and sorry for my english

Albert.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to validate input values

Hi How would i validate value of a variable whether it is number,date or string Thanks in advance Sas (5 Replies)
Discussion started by: SasDutta
5 Replies

2. Shell Programming and Scripting

Need to validate a date input format

Hi all, I have a shell script(K shell) which takes a date as input. i want the input to be in DD-MM-YYYY format. Can i enforce such a format of input string using just one line of code? OR do i need to parse the input date into different components and test them using Case statements... (2 Replies)
Discussion started by: rajugp1
2 Replies

3. Shell Programming and Scripting

validate input

the user inputs names that have to be inside square brackets I want to check if the user puts the brackets and if not ask him to re-enter the names (9 Replies)
Discussion started by: DDoS
9 Replies

4. Shell Programming and Scripting

Validate and sort input

Hi, This will most likely be a simple answer. Currently I have a situation where my script will be sent various options: -o1 -o2 -oe3@somthing.com Now, if I want to run a certain command based on the option I am sent, I am doing the following. for o in $(echo $options) do if ... (3 Replies)
Discussion started by: stuaz
3 Replies

5. Shell Programming and Scripting

How to validate input parameters?

Hi, I wonder how I can know if the input parameters to the script are numbers or text Thanks (11 Replies)
Discussion started by: Gengis-Kahn
11 Replies

6. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

7. Shell Programming and Scripting

Another validate input Question.

I'm writing a bash shell script to 'help' me post to susepaste (I can NEVER remember the time options). Here's the code: #!/bin/bash ########## # # Project : personal script. # Started : Wed Aug 03, 2011 # Author : Habitual # Description : susepaste c-li script with user... (5 Replies)
Discussion started by: Habitual
5 Replies

8. Shell Programming and Scripting

Epic - Validate input size

Is there an easy way to validate an input field size. Let us say a script is asking to enter 10 digits mobile number, how do I write a script to validate it is numeric and is 10 digits in length? I just need an easy way w/o using looks ...etc. Is there such a away ? Here is what I have so far... (6 Replies)
Discussion started by: mrn6430
6 Replies

9. Shell Programming and Scripting

How to validate user's input..?

$Input_filename=$ARGV; if (!-d $Input_filename && ! -e $Input_filename) { print "USAGE: Please enter '$ABCD/def/dsed.txt' as an arguement \n"; exit; } 1. Input Is suppose to be something like "$ABCD/def/dsed.txt". if the input is wrong the script should throw an ERROR message.... (2 Replies)
Discussion started by: Rashid Khan
2 Replies

10. Shell Programming and Scripting

Validate input files and update

We have a job which we need to run on daily bases, before loading data in a table we need to validate whether the input file is received or not.Inputfile formatsrc_sps_d_Call_Center_Reporting_yyyymmdd_01.dat SPS-Service nameYYYY-yearMM-MonthDD-dayLike above we will get n number of files for... (1 Reply)
Discussion started by: katakamvivek
1 Replies
GETOPT(1)						    BSD General Commands Manual 						 GETOPT(1)

NAME
getopt -- parse command options SYNOPSIS
args=`getopt optstring $*` set -- `getopt optstring $*` DESCRIPTION
getopt is used to break up options in command lines for easy parsing by shell procedures, and to check for legal options. [Optstring] is a string of recognized option letters (see getopt(3)); if a letter is followed by a colon, the option is expected to have an argument which may or may not be separated from it by white space. The special option ``--'' is used to delimit the end of the options. getopt will place ``--'' in the arguments at the end of the options, or recognize it if used explicitly. The shell arguments ($1, $2, ...) are reset so that each option is preceded by a ``-'' and in its own shell argument; each option argument is also in its own shell argument. getopt should not be used in new scripts; use the shell builtin getopts instead. EXAMPLES
The following code fragment shows how one might process the arguments for a command that can take the options [a] and [b], and the option [c], which requires an argument. args=`getopt abc: $*` if [ $? -ne 0 ]; then echo 'Usage: ...' exit 2 fi set -- $args while [ $# -gt 0 ]; do case "$1" in -a|-b) flag=$1 ;; -c) carg=$2; shift ;; --) shift; break ;; esac shift done This code will accept any of the following as equivalent: cmd -acarg file file cmd -a -c arg file file cmd -carg -a file file cmd -a -carg -- file file IEEE Std 1003.2 (``POSIX.2'') mandates that the sh(1) set command return the value of 0 for the exit status. Therefore, the exit status of the getopt command is lost when getopt and the sh(1) set command are used on the same line. The example given is one way to detect errors found by getopt. DIAGNOSTICS
getopt prints an error message on the standard error output when it encounters an option letter not included in [optstring]. SEE ALSO
sh(1), getopt(3) HISTORY
Written by Henry Spencer, working from a Bell Labs manual page. Behavior believed identical to the Bell version. BUGS
Whatever getopt(3) has. Arguments containing white space or embedded shell metacharacters generally will not survive intact; this looks easy to fix but isn't. The error message for an invalid option is identified as coming from getopt rather than from the shell procedure containing the invocation of getopt; this again is hard to fix. The precise best way to use the set command to set the arguments without disrupting the value(s) of shell options varies from one shell ver- sion to another. BSD
November 28, 2009 BSD
All times are GMT -4. The time now is 08:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy