Check if any of the parameters to a bash script match a string
I'm trying to write a script where I want to check if any of the parameters passed to a bash script match a string. The way I have it setup right now is
if [ "$3" != "-disCopperBld" -a "$4" != "-disCopperBld" -a "$5" != "-disCopperBld" -a "$6" != "-disCopperBld"]
but there might be a large number of parameters, so I was wondering if there is a better way to do this?
Thanks
EDIT: I tried this chunk of code out, and called the script with the option, -disableVenusBld, but it still prints out "Starting build". Am I doing something wrong? Thanks in advance!
while [ $# -ne 0 ]
do
arg="$1"
case "$arg" in
-disableVenusBld)
disableVenusBld=true
;;
-disableCopperBld)
disableCopperBld=true
;;
-disableTest)
disableTest=true
;;
-disableUpdate)
disableUpdate=true
;;
*)
nothing="true"
;;
esac
shift
done
if [ "$disableVenusBld" != true ]; then
echo "Starting build"
fi
Solution 1:
It looks like you're doing option handling in a shell script. Here's the idiom for that:
#! /bin/sh -
# idiomatic parameter and option handling in sh
while test $# -gt 0
do
case "$1" in
--opt1) echo "option 1"
;;
--opt2) echo "option 2"
;;
--*) echo "bad option $1"
;;
*) echo "argument $1"
;;
esac
shift
done
exit 0
(There are a couple of conventions for indenting the ;;
, and some shells allow you to give the options as (--opt1)
, to help with brace matching, but this is the basic idea)
Solution 2:
This worked for me. It does exactly what you asked and nothing more (no option processing). Whether that's good or bad is an exercise for the poster :)
if [[ "$*" == *YOURSTRING* ]]
then
echo "YES"
else
echo "NO"
fi
This takes advantage of special handling of $*
and bash super-test [[
…]]
brackets.
Solution 3:
How about searching (with wildcards) the whole parameter space:
if [[ $@ == *'-disableVenusBld'* ]]
then
Edit: Ok, ok, so that wasn't a popular answer. How about this one, it's perfect!:
if [[ "${@#-disableVenusBld}" = "$@" ]]
then
echo "Did not find disableVenusBld"
else
echo "Found disableVenusBld"
fi
Edit2: Ok, ok, maybe this isn't perfect... Think it works only if -param is at the start of the list and will also match -paramXZY or -paramABC. I still think the original problem can be solved very nicely with bash string manipulation, but I haven't quite cracked it here... -Can you??
Solution 4:
disCopperBld=
for x; do
if [ "$x" = "-disCopperBld" ]; then disCopperBld=1; break; fi
done
if [ -n "$disCopperBld" ]; then
...
fi
If you need to test only the parameters starting at $3
, do the search in a function:
## Usage: search_trailing_parameters NEEDLE NUM "$@"
## Search NEEDLE amongst the parameters, skipping $1 through ${$NUM}.
search_trailing_parameters () {
needle=$1
shift $(($2 + 2))
for x; do
if [ "$x" = "$needle" ]; then return 0; fi
done
return 1
}
if search_trailing_parameters -disCopperBld 2 "$@"; then
...
fi
But I wonder why you're trying to do this in the first place, it's not a common need. Usually, you'd process options in order, as in Dennis's answer to your previous question.