Last active
January 26, 2017 01:02
-
-
Save whitekid/72ce0e1f637eae6ae0bfdc77bdcce14b to your computer and use it in GitHub Desktop.
bash extra argement(--) parsing with getopts
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 | |
# | |
# extra_args.sh -l args script_args -- -l extra args | |
# | |
# OUTPUT | |
# opt: l=args | |
# arg: %script_args% | |
# extra_args: %-l extra args% | |
# | |
while getopts 'l:' opt; do | |
case $opt in | |
l) | |
echo "opt: $opt=$OPTARG";; | |
esac | |
done | |
shift $((OPTIND-1)) | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
--) | |
shift; | |
extra_args="$@" | |
break;; | |
*) | |
args+="$1 ";; | |
esac | |
shift | |
done | |
args="${args/% /}" | |
echo "arg: %$args%" | |
echo "extra_args: %$extra_args%" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment