[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Setting variable with getopts
From: |
hancooper |
Subject: |
Setting variable with getopts |
Date: |
Mon, 09 Aug 2021 18:10:32 +0000 |
Have updated some things so I can use the following command
to set vb to 358, with the rest of the arguments being arg1 arg2
myfunc -v 358 arg1 arg2
And want to have
myfunc -v arg1 arg2
use default for vb
-----
These are the results
myfunc-status -v 238
arg: v
vb: 238 # correct value set to vb
$@ # correct, no arguments left
myfunc -v 238 arg1 arg2
arg: v
vb: 1 # incorrect, v must be 358
$@ 238 arg1 arg2 # incorrect arguments, including 358
getopt-status -v arg1 arg2
arg: v
vb: 1 # gives correct default
$@ arg1 arg2 # gives correct arguments
-- here is the code
myfunc ()
{
local vb=123
local OPTIND OPTARG
local shortopts="Vuhv"
while getopts $shortopts arg; do
echo "arg: $arg"
case $arg in
# ........................................................
("V")
printf "%s\n" "Version"
return
;;
("u")
printf "%s\n" "-V, -u, -h"
printf "%s\n" "getopt-status --"
return
;;
("h")
printf "%s\n" "Prints status returned by getopt."
printf "%s\n" "-V Displays version"
printf "%s\n" "-u Displays usage"
printf "%s\n" "-h Displays help."
printf "%s\n" "-v, -v NUM Sets verbosity level."
return
;;
("v")
if [[ ${@:$OPTIND} =~ ^[0-9]+$ ]]; then
vb=${@:$OPTIND:1}
OPTIND=$((OPTIND+1))
else
vb=1
fi
;;
esac
done
local n="$((OPTIND-1))"
shift "$n"
echo "vb: $vb"
echo "\$@ $@"
}
- Setting variable with getopts, hancooper, 2021/08/09
- Setting variable with getopts, hancooper, 2021/08/09
- Re: Setting variable with getopts, Dennis Williamson, 2021/08/09
- Re: Setting variable with getopts, hancooper, 2021/08/09
- Setting variable with getopts, hancooper, 2021/08/09
- Re: Setting variable with getopts, Dennis Williamson, 2021/08/09
- Re: Setting variable with getopts, Dennis Williamson, 2021/08/09
- Setting variable with getopts, hancooper, 2021/08/09
- Setting variable with getopts,
hancooper <=
- Re: Setting variable with getopts, Dennis Williamson, 2021/08/09
- Setting variable with getopts, hancooper, 2021/08/09