In my script I have 3 mandatory parameters , host (-s), key(-k) ,prop_name(-p) but I have two conditions , either one would be entered by the user while running the script
Note that a ( command; command ) grouping would work but would enforce a sub shell: extra fork() overhead, and modified parameters cannot be retrieved in the main shell.
For covering "if -v is present then throw error if either -b or -e are present" this scheme extends to
where the prop_val is queried twice. It is smarter to use an if-else-fi where the last condition (in the body) determines the resulting status:
if
[ -z "$host" ] || [ -z "$key" ] || [ -z "$prop_name" ] || {
if [ -z "$prop_val" ]
then
[ -z "$begin_val" ] || [ -z "$end_val" ]
else
[ -n "$begin_val" ] || [ -n "$end_val" ]
fi
}
then
Last but not least, if you have a usage function, then you can call it at different points, and add individual messages:
if [ -z "$host" ]; then
echo "missing -s host"
usage; exit 1
fi
if [ -z "$key" ]; then
echo "missing -k key"
usage; exit 1
fi
if [ -z "$prop_name" ]; then
echo "missing -p prop_name"
usage; exit 1
fi
if [ -z "$prop_val" ]; then
if [-z "$begin_val" ] || [ -z "$end_val" ]; then
echo "need both -b begin_val and -e end_val"
usage; exit 1
fi
else
if [ -n "$begin_val" ] || [ -n "$end_val" ]; then
echo "either -v prop_val or -b begin_val and -e end_val"
usage; exit 1
fi
fi