Reputation: 91
I wrote the following shellscript. I am trying to get inputs from the user using the getopts method. This is the getopts fragment of code that I wrote.
#Define the help function
function help(){
echo "Options:";
echo "-u Github username"
echo "-p Github password"
echo "-r Repository name"
echo "-s Service name"
echo "-b Branch name (Default master)"
exit 1;
}
#Initialize the default values for the variables.
username="username";
password="password";
rname="rname";
sname="sname";
branch="master";
#Define the getopts variables
options="u:p:r:s:h";
#Start the getopts code
while getopts options opt; do
case $opt in
u) #Get the username
username=$OPTARG
;;
p) #Get the password
password=$OPTARG
;;
r) #Get the repository name
rname=$OPTARG
;;
s) #Get the service name
sname=$OPTARG
;;
b) #Get the branch name
branch=$OPTARG
;;
h) #Execute the help function
"echo here"
help;
;;
\?) #unrecognized option - show help
echo "Invalid option."
help;
;;
esac
done
#This tells getopts to move on to the next argument.
shift $((OPTIND-1))
#End getopts code
I tried to start the script by:
./testScript.sh -u myname
I get the following error:
illegal option -- u
Upvotes: 3
Views: 1737
Reputation: 733
You forgot the dollar sign in front of options to consider it a variable:
#Start the getopts code
while getopts $options opt; do
Al
Upvotes: 2
Reputation: 17041
while getopts "$options" opt
# ^^ ^
$
substitutes in the value of the variable options
. Without the $
, getopts
thinks the valid flags are -o, -p, -t, -i, -n, and -s.
Upvotes: 4