Reputation: 30990
I have a bashscript that expects an optional argument as follows, there are several things that I would like to refine it so that I can pass in the first argument in different ways(0, false to indicate a false value, all other string to indicate true, defaults to false), what's an elegant way to script this? I would like to use if statement with the right boolean expression.
if [ -n "$1" ]; then update_httpconf="$1"; fi if [ "$update_httpconf" = "true" ]; then echo "hi"; fi
Upvotes: 5
Views: 2614
Reputation: 2151
If you made it so that making the first argument --update-httpconf
counts as true, and anything else counts as false, it would be more consistent with other unixey command line utilities.
This might be a good starting point:
while [ $# -gt 0 ]
do
case $1 in
'--update-httpconf')
update_httpconf=true
;;
*)
echo "unrecognised arg $1"; exit
;;
esac
shift
done
Upvotes: 3
Reputation: 62369
Use a case
statement:
case "$1" in
0|false|FALSE) do_something;;
*) do_something_else;;
esac
Upvotes: 0