Skip to content

Instantly share code, notes, and snippets.

@paxperscientiam
Forked from un33k/bash-args-options
Created July 9, 2017 01:03
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 paxperscientiam/75cfb2efb63e016a7cc39e900d607192 to your computer and use it in GitHub Desktop.
Save paxperscientiam/75cfb2efb63e016a7cc39e900d607192 to your computer and use it in GitHub Desktop.
Bash - Simple Skeleton for arg parsing in bash
#!/bin/bash
# Argument = -t test -r server -p password -v
usage()
{
cat << EOF
usage: $0 options
This script run the test1 or test2 over a machine.
OPTIONS:
-h Show this message
-t Test type, can be ‘test1′ or ‘test2′
-r Server address
-p Server root password
-v Verbose
EOF
}
TEST=
SERVER=
PASSWD=
VERBOSE=
while getopts “ht:r:p:v” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
t)
TEST=$OPTARG
;;
r)
SERVER=$OPTARG
;;
p)
PASSWD=$OPTARG
;;
v)
VERBOSE=1
;;
?)
usage
exit
;;
esac
done
if [[ -z $TEST ]] || [[ -z $SERVER ]] || [[ -z $PASSWD ]]
then
usage
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment