Created
February 15, 2023 03:08
-
-
Save bukowa/8260154f09b51fec793778af09e56a53 to your computer and use it in GitHub Desktop.
bash cli with positional and named arguments
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/bash | |
set -o errexit | |
force=false | |
reg_name='test' | |
reg_port='5001' | |
pos_args=() | |
while [ $# -gt 0 ]; do | |
arg="$1" | |
if [[ ${arg:0:1} != "-" ]]; then | |
# handle positional argument | |
pos_args+=("$arg") | |
else | |
case $arg in | |
-h|--help) | |
echo "options:" | |
echo "-h, --help show brief help" | |
echo "-f, --force to force recreate cluster" | |
echo "-rn, --regname registry name" | |
echo "-rp, --regport registry port" | |
exit 0 | |
;; | |
-f|--force) | |
force=true | |
;; | |
-rn|--regname) | |
reg_name=$2 | |
shift | |
;; | |
-rp|--regport) | |
reg_port=$2 | |
shift | |
;; | |
*) | |
echo "Unknown option: $arg" | |
exit 1 | |
;; | |
esac | |
fi | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment