-
-
Save AnderRasoVazquez/16d460f646e949201ca9 to your computer and use it in GitHub Desktop.
[Getopts in Bash] #bash #shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
while getopts ab:c: opt | |
do | |
case $opt in | |
a) | |
flga=1 | |
;; | |
b) | |
flgb=1 | |
optb=$OPTARG | |
;; | |
c) | |
flgc=1 | |
optc=$OPTARG | |
;; | |
\?) | |
;; | |
esac | |
done | |
shift $(($OPTIND - 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
OPT=`getopt -n $0 -o ab:c: --long aaa,bbb:,ccc: -- "$@"` | |
eval set -- "$OPT" | |
while true; | |
do | |
case $1 in | |
-a|--aaa) | |
opt_a=true | |
;; | |
-b|--bbb) | |
opt_b=$2 | |
;; | |
-c|--ccc) | |
opt_c=true | |
;; | |
--) | |
break | |
;; | |
esac | |
shift | |
done | |
echo $opt_a | |
echo $opt_b | |
echo $opt_c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment