Created
August 25, 2023 10:17
-
-
Save motey/5e57d84768a739c84826387af40ca008 to your computer and use it in GitHub Desktop.
Bash script parse named arguments
This file contains hidden or 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 | |
# My function header... | |
# Arguments: | |
# "--uid" or "-u" -> uid - int - default: 0 - user id for... | |
# "--gid" or "-g" -> gid - int - default: 0 - group id for... | |
# parse arguments | |
while [ $# -gt 0 ]; do | |
case "${1%%=*}" in | |
-u | --uid) | |
arg_uid="${1#*=}" | |
;; | |
-g | --gid) | |
arg_gid="${1#*=}" | |
;; | |
*) | |
printf "Error: Invalid argument: '${1%%=*}' \n" | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
# set argument defaults if necessary | |
if [ -z "$arg_uid" ]; then | |
arg_uid=0 | |
fi | |
if [ -z "$arg_gid" ]; then | |
arg_gid=0 | |
fi | |
echo "uid: $arg_uid" | |
echo "gid: $arg_gid" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage: