scripting - Bash with getopts/flags and multiple functions -


i have problem getopts. when write ./nameofscript.sh -n name - should make new repository says me : -- n unknown option , in new line: adding user name: blank.

why happen ?

    #!/bin/bash       #what2do=${0##*/}     #what2do=${what2do%.sh}       what2do="addproject"     what2do="addrepository"     what2do="adduser"       doaddrepository ()  {      local reponame="$1"     echo "adding repo name: $reponame"      # adding new repository name $reponame...     }     doaddproject ()  {      local projname="$1"     echo "adding project name: $projname"      # adding new project name $projname...     }     doadduser ()  {      local username="$1"     echo "adding user name: $username"      # code add user "$username"...     }  case $what2do in addrepository)       while getopts 'n:' key;         case $key in           n) repo_name=$optarg ;;         esac       done       doaddrepository "$repo_name"       exit $?     ;; addproject)       while getopts 'm:' key;         case $key in           m) proj_name=$optarg ;;         esac       done       doaddproject "$proj_name"       exit $?     ;; adduser)       while getopts 'u:' key;         case $key in           u) user_name=$optarg ;;         esac       done       doadduser "$user_name"       exit $?     ;;     *)       echo "i don't know how $what2do. maybe, niy" >&2     #  doshowusage       exit 1     ;; esac 

the problem define what2do variable 3 times @ beginning of code:

what2do="addproject" what2do="addrepository" what2do="adduser 

and final value of what2do variable adduser. , following code executed in case block. expects -u option.

enteradduser)   while getopts 'u:' key;     case $key in       u) user_name=$optarg ;;     esac   done   doadduser "$user_name"   exit $? 

following code sample:

    #!/bin/bash      what2do="addproject"     what2do="addrepository"     what2do="adduser"       doaddrepository ()  {      local reponame="$1"     echo "adding repo name: $reponame"      # adding new repository name $reponame...     }     doaddproject ()  {      local projname="$1"     echo "adding project name: $projname"      # adding new project name $projname...     }     doadduser ()  {      local username="$1"     echo "adding user name: $username"      # code add user "$username"...     }  getopts 'm:' mode modevalue=$optarg  getopts 'p:' paremter parametervalue=$optarg  case $modevalue in addrepository)       doaddrepository "$parametervalue"       exit $?     ;; addproject)       doaddproject "$parametervalue"       exit $?     ;; adduser)       doadduser "$parametervalue"       exit $?     ;;     *)       echo "i don't know how $modevalue. maybe, niy" >&2     #  doshowusage       exit 1     ;; esac 

and following usage:

./script.sh -m addproject -p projectname ./script.sh -m adduser -p username ./script.sh -m addrepository -p repositoryname 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -