adamos
adamos

Reputation: 69

Bash script with getopt - long options

so far i have edited my script and it doesn't work as i would like to.

Firstly i will present this code:

    while true ; do
      case "$1" in
        --mode)
           MODE=$2
           shift 2;         
           ;;
        -l|--login)
           LOGIN=$2 
           shift 2;
           ;;
        -e|--email) 
           EMAIL=$2
           shift 2; 
           ;;
        -r|--repo)
           REPO=$2
           shift 2; 
           ;;
        -p|--project)
           PROJ="$2"
           shift 2;
           ;;
       -h|--help)
           echo "$doShowUsage"
           exit 1
   esac
   break
done

if [ "$MODE" == "doAddRepository" ]; then
        echo " CREATING REPOS!!!"
    doAddRepository "$PROJ" "$REPO"
fi

if [ "$MODE" == "doAddProject" ]; then
    doAddProject "$PROJ"
fi

As you can see above when i write in cli:

./script.sh --mode doAddProject -p test

It will not work but when i change the code for:

  if [ "$MODE" == "doAddProject" ]; then
    doAddProject "$2"
fi

Then it works! My question is how to connect with my functions ? I present below 2 of the functions.

doAddRepository{
local projectName=$1
local repoName=$2
...
}

doAddProject{
local projectName=$1
...
}

TEMP=`getopt -o m:p:r:h --long mode:,project:,repo:,help -n 'script.sh' -- "$@"`
eval set -- "$TEMP"

Upvotes: 0

Views: 543

Answers (1)

Vishal
Vishal

Reputation: 1209

Your loop is iterating only once because of the out of place break statement. You should break the loop only if the argument is empty.

while true ; do
    case "$1" in
        --mode)
            MODE=$2
            shift 2;         
            ;;
        -l|--login)
            LOGIN=$2 
            shift 2;
            ;;
        -e|--email) 
            EMAIL=$2
            shift 2; 
            ;;
        -r|--repo)
            REPO=$2
            shift 2; 
            ;;
        -p|--project)
            PROJ="$2"
            shift 2;
            ;;
       -h|--help)
            echo "$doShowUsage"
            exit 1
            ;;
        '')
            break
            ;;
   esac
done

Upvotes: 1

Related Questions