Skip to content

Instantly share code, notes, and snippets.

@si9ma
Last active May 5, 2019 06:35
Show Gist options
  • Save si9ma/790186c4563ab2fcadbb5095b0cb1e53 to your computer and use it in GitHub Desktop.
Save si9ma/790186c4563ab2fcadbb5095b0cb1e53 to your computer and use it in GitHub Desktop.
parse shell function arguments
fun() {
# usage
read -r -d '' usage << EOM
usage: $0 [-h] [-u=<user>] [-e] [-p=<port>] host
-h help
-u user name
-p ssh port
-e if remote editable?
EOM
# variable
local host= user=root port=22 edit=
# parse arguments
while [ "$1" != "" ]; do
PARAM=`cut -d '=' -f 1 <<< "$1"`
VALUE=`cut -d '=' -f 2- <<< "$1"`
case $PARAM in
-h)
echo $usage
return
;;
-u)
user="$VALUE"
;;
-p)
port="$VALUE"
;;
-e)
edit="true"
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
echo $usage && return
;;
esac
shift
done
# logic
[ "$host" = "" ] && echo "[ERROR] Please provide host" && echo $usage && return
ssh -p $post $user@$host
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment