Skip to content

Instantly share code, notes, and snippets.

@mralexgray
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mralexgray/bb67d31aafd4f3217c1f to your computer and use it in GitHub Desktop.
Save mralexgray/bb67d31aafd4f3217c1f to your computer and use it in GitHub Desktop.
Canonical shell argument parsing.
#!/usr/bin/env zsh
# Print usage information if there are no arguments on the command line.
[ "$#" = "0" ] &&
echo 'Usage: via-ssh.sh [ -v ] [ -d ] [USER@]SERVER[:PORT]' && exit 1
# Process the command line parameters.
while [ "$#" != '0' ] ; do
case "$1" in
-v) VERBOSE=1 ; shift ;;
-d) DNS=1 ; shift ;;
*) SSH_PORT=${1##*:}
SSH_USER_SERVER=${1%%:*}
[ "$SSH_USER_SERVER" = "$SSH_PORT" ] && SSH_PORT=22
shift ;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment