Manipulating arguments with OPTIND, OPTARG, getopts, and shift correctly
0
votes
1
answer
146
views
My small POSIX shell scripts do not usually take any arguments, so this is kind of new to me...
The minimal snippet would probably look like this:
# default for hotkey variable on top of script is set
hotkey=Print
...
while getopts ':hk:' option; do
case "$option" in
k) # override default hotkey variable with supplied arg.
hotkey=$OPTARG
shift 2
;;
h) # self-explanatory I assume, prints usage, and exits script with code 0
print_usage_and_exit 0
;;
*) # inspects unspecified arguments, prints usage, and exits script with code 1
dump_args "$@"
print_usage_and_exit 1
;;
esac
done
...
What remains unclear to me, if in this particular case, there is any use for the notoriety known command:
shift $(( OPTIND - 1 ))
Thanks for any direction
Asked by Vlastimil Burián
(30515 rep)
Jun 16, 2024, 04:10 PM
Last activity: Jul 23, 2024, 02:39 PM
Last activity: Jul 23, 2024, 02:39 PM