Created
March 10, 2021 18:52
-
-
Save valtoni/56d6b83a0614e8f82c2ee13fcf5e6abd to your computer and use it in GitHub Desktop.
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
cat >/tmp/demo-space-separated.sh <<'EOF' | |
#!/bin/bash | |
POSITIONAL=() | |
while [[ $# -gt 0 ]] | |
do | |
key="$1" | |
case $key in | |
-e|--extension) | |
EXTENSION="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-s|--searchpath) | |
SEARCHPATH="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-l|--lib) | |
LIBPATH="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--default) | |
DEFAULT=YES | |
shift # past argument | |
;; | |
*) # unknown option | |
POSITIONAL+=("$1") # save it in an array for later | |
shift # past argument | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" # restore positional parameters | |
echo "FILE EXTENSION = ${EXTENSION}" | |
echo "SEARCH PATH = ${SEARCHPATH}" | |
echo "LIBRARY PATH = ${LIBPATH}" | |
echo "DEFAULT = ${DEFAULT}" | |
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l) | |
if [[ -n $1 ]]; then | |
echo "Last line of file specified as non-opt/last argument:" | |
tail -1 "$1" | |
fi | |
EOF | |
chmod +x /tmp/demo-space-separated.sh | |
/tmp/demo-space-separated.sh -e conf -s /etc -l /usr/lib /etc/hosts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As see in https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash