Validating command line arguments in shell script

Posted by / 09-Feb-2015 10:54

expands to the positional parameters, starting from one.

When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable.

All of these features involve using command line options and arguments.

It's working fine except I'd like to add some logic to ensure only correct arguments are entered.

I've tried the below statement in multiple ways, but I'm not having any luck. I expected the qa, qa2 etc to be dummy values for more complex tests.

Thanks EDIT: I tried this chunk of code out, and called the script with the option, -disable Venus Bld, but it still prints out "Starting build". = true ]; then echo "Starting build" fi Thanks for the replies guys, I appreciate ya'll taking time out to help me.

I tried a chunk of code, but I can't seem to figure out what's going wrong. (I've pasted the code in an edit of my original post)#!

/bin/sh - # idiomatic parameter and option handling in sh while test $# -gt 0 do case "" in --opt1) echo "option 1" ;; --opt2) echo "option 2" ;; --*) echo "bad option " ;; *) echo "argument " ;; esac shift done exit 0 ## Usage: search_trailing_parameters NEEDLE NUM "[email protected]" ## Search NEEDLE amongst the parameters, skipping through ${$NUM}.

If they are just actual values, the solution of @Gnouc is much better obviously.

I'll leave this answer here for educational value related to the original examples syntax. This is not strictly necessary inside of the double , but almost anywhere else, for example in [ ], which are very similar.

"Usage: [[ , but almost anywhere else, for example in [ ], which are very similar.

"Usage: $0 ARGUMENT"} # Script exits here if command-line parameter absent, #+ with following error message.

I'm working on a bash script to restart multiple apache instances in different environments.

It's hard enough to get used to always quote, so I'll just keep the quotes.

||

If they are just actual values, the solution of @Gnouc is much better obviously.I'll leave this answer here for educational value related to the original examples syntax. This is not strictly necessary inside of the double , but almost anywhere else, for example in [ ], which are very similar. "Usage: $0 ARGUMENT"} # Script exits here if command-line parameter absent, #+ with following error message.I'm working on a bash script to restart multiple apache instances in different environments.It's hard enough to get used to always quote, so I'll just keep the quotes.

]] ARGUMENT"} # Script exits here if command-line parameter absent, #+ with following error message.

I'm working on a bash script to restart multiple apache instances in different environments.

It's hard enough to get used to always quote, so I'll just keep the quotes.

validating command line arguments in shell script-67validating command line arguments in shell script-25validating command line arguments in shell script-75

but there might be a large number of parameters, so I was wondering if there is a better way to do this? while [ $# -ne 0 ] do arg="$1" case "$arg" in -disable Venus Bld) disable Venus Bld=true ;; -disable Copper Bld) disable Copper Bld=true ;; -disable Test) disable Test=true ;; -disable Update) disable Update=true ;; *) nothing="true" ;; esac shift done if [ "$disable Venus Bld" !