Last active
May 5, 2019 06:35
-
-
Save si9ma/790186c4563ab2fcadbb5095b0cb1e53 to your computer and use it in GitHub Desktop.
parse shell function arguments
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
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