bash script argument parsing code example
Example: bash argument parsing
cat >/tmp/demo-space-separated.sh <<'EOF'
POSITIONAL=()
while [[ $
do
key="$1"
case $key in
-e|--extension)
EXTENSION="$2"
shift
shift
;;
-s|--searchpath)
SEARCHPATH="$2"
shift
shift
;;
-l|--lib)
LIBPATH="$2"
shift
shift
;;
--default)
DEFAULT=YES
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
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