help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getopts with OPTIND - 1


From: nigelberlinguer
Subject: getopts with OPTIND - 1
Date: Sat, 28 Aug 2021 00:40:37 +0000

I have the following test with options being handled by `getopts'.
At the end I shift with $(( OPTIND - 1 )), followed by printing "$@".

Strange things happen when using

$ test -s dhoehu oehdu hoeu oehu doehu
dhoehuvb: 1

test ()
{

local vb=1 sort=0

local OPTIND OPTARG
local shortopts="Vuhv:s"
while getopts $shortopts arg; do
case $arg in
("V")
printf '%s\n' "Version"
return
;;
("u")
printf '%s\n' "usage"
return
;;
("h")
printf '%s\n' "help"
return
;;
("v") vb="$OPTARG" ;;
#.............................
("s") sort=1 ;;
#.............................
(?)
pfm "Invalid option: -${OPTARG}."
pfm "Invoke \`test -h\' for details."
break
;;
esac
done

shift $(( OPTIND - 1 ))
printf "$@"

reply via email to

[Prev in Thread] Current Thread [Next in Thread]