0

I have problem with getopts. When i write ./nameofscript.sh -n name - it should make a new repository but it says to me : -- n is unknown option and in new line: Adding user with name: blank.

Why does it happen ?

    #!/bin/bash


    #what2do=${0##*/}
    #what2do=${what2do%.sh}


    what2do="addProject"
    what2do="addRepository"
    what2do="addUser"


    doAddRepository ()  {
     local repoName="$1"
    echo "Adding repo with name: $repoName"
     # Adding new repository with the name $repoName...
    }
    doAddProject ()  {
     local projName="$1"
    echo "Adding project with name: $projName"
     # Adding new project with the name $projName...
    }
    doAddUser ()  {
     local userName="$1"
    echo "Adding user with name: $userName"
     # Some code to add user "$userName"...
    }

case $what2do in
addRepository)
      while getopts 'n:' key; do
        case $key in
          n) REPO_NAME=$OPTARG ;;
        esac
      done
      doAddRepository "$REPO_NAME"
      exit $?
    ;;
addProject)
      while getopts 'm:' key; do
        case $key in
          m) PROJ_NAME=$OPTARG ;;
        esac
      done
      doAddProject "$PROJ_NAME"
      exit $?
    ;;
addUser)
      while getopts 'u:' key; do
        case $key in
          u) USER_NAME=$OPTARG ;;
        esac
      done
      doAddUser "$USER_NAME"
      exit $?
    ;;
    *)
      echo "I don't know how to $what2do. Maybe, it is NIY" >&2
    #  doShowUsage
      exit 1
    ;;
esac
adamos
  • 69
  • 8

1 Answers1

0

The problem is you define what2do variable 3 times at the beginning of your code:

what2do="addProject"
what2do="addRepository"
what2do="addUser

And final value of what2do variable is addUser. And following code is executed in case block. Which expects -u option.

enteraddUser)
  while getopts 'u:' key; do
    case $key in
      u) USER_NAME=$OPTARG ;;
    esac
  done
  doAddUser "$USER_NAME"
  exit $?

Following is code sample:

    #!/bin/bash

    what2do="addProject"
    what2do="addRepository"
    what2do="addUser"


    doAddRepository ()  {
     local repoName="$1"
    echo "Adding repo with name: $repoName"
     # Adding new repository with the name $repoName...
    }
    doAddProject ()  {
     local projName="$1"
    echo "Adding project with name: $projName"
     # Adding new project with the name $projName...
    }
    doAddUser ()  {
     local userName="$1"
    echo "Adding user with name: $userName"
     # Some code to 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 to $modeValue. Maybe, it is NIY" >&2
    #  doShowUsage
      exit 1
    ;;
esac

And following is usage:

./script.sh -m addProject -p ProjectName
./script.sh -m addUser -p UserName
./script.sh -m addRepository -p RepositoryName
Murad Tagirov
  • 776
  • 6
  • 10
  • Okey so how can I get rid of this problem? Should i make 3 different cases and define at the beginning 3 another variables? For example: what2do, what2do2, what2do3 ? Is this solution? – adamos Aug 05 '15 at 10:45
  • In my opinion your script could read two paremeters from input. Like ./script.sh -m -p parameter. And then use your script like: To add project for example: ./script.sh -m project -p project_name – Murad Tagirov Aug 05 '15 at 10:48
  • Can you show me in code how do you see it ? What about variable "what2do"? – adamos Aug 05 '15 at 11:24
  • Thank you Murad ! You are great !! I have got just one more question. What about if i got few parameters in adding user ? For example i need to add: Username , Name, Password and email ? How about adding more parameters and flags for them ? – adamos Aug 05 '15 at 12:06
  • You can do it same way by adding more parameters. Then parse and verify all input parameters at script startup. – Murad Tagirov Aug 05 '15 at 12:27
  • Okey i did it and it works:) What about making long flags to make script running more clear ? Could you show me one of option how it should look like ? – adamos Aug 05 '15 at 12:45
  • Please see following link: http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options – Murad Tagirov Aug 05 '15 at 12:47