vinod siragaon
vinod siragaon

Reputation: 1

getting syntax error in case statement in shell

#!/bin/bash

branch=$1
vcva=$2
esx=$3
pxe=$4
setup=$5




#If branch is Vsphere-2015

if [ "$branch" == "vsphere2015"];then

      echo " Running Bats for Vsphere-2015 with the following details ."



if [ ! "vcva" ];then

    echo "VCVA Build is $2 "
    echo "ESX Build is $4 "
    echo "pxe info is $5 "


#If all the setups has to be run

setup=$5
case "$setup" in "all")

        echo "runnning all setups on Vsphere-2015."
        vpshere2015_primary
        vpshere2015_M1N1
        vpshere2015_M2N1                               =======> these are methods 
        vpshere2015_legacy
    ;;

I'm new to shell and after this piece of code getting

bat.sh: line 38: syntax error near unexpected token newline' 'at.sh: line 38: ;;

I want to run some functions depending upon the inputs given by the user in command line

Upvotes: 0

Views: 1592

Answers (2)

You just have to close all the if's (with "fi") and the case statement (with "esac").

I thing you also have to change the

if [ ! "vcva" ];then

into

if [ ! -z "$vcva" ] ; then

It will result something like:

#!/bin/bash

branch=$1
vcva=$2
esx=$3
pxe=$4
setup=$5

#If branch is Vsphere-2015
if [ "$branch" == "vsphere2015" ];then
  echo " Running Bats for Vsphere-2015 with the following details ."
  if [ ! -z "$vcva" ];then
    echo "VCVA Build is $2 "
    echo "ESX Build is $4 "
    echo "pxe info is $5 "

    #If all the setups has to be run
    setup=$5
    case "$setup" in 
      "all")    
        echo "runnning all setups on Vsphere-2015."
        vpshere2015_primary
        vpshere2015_M1N1
        vpshere2015_M2N1
        vpshere2015_legacy
      ;;
      *) 
        echo "default action goes here"
      ;;
    esac
  fi # close second if
fi # close first if

Upvotes: 1

nu11p01n73R
nu11p01n73R

Reputation: 26667

The case satatement must end with esac

Syntax of case is

case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;; ... CASEN) COMMAND-LIST;; esac

So here it should be

case "$setup" in "all")

        echo "runnning all setups on Vsphere-2015."
        vpshere2015_primary
        vpshere2015_M1N1
        vpshere2015_M2N1                               =======> these are methods 
        vpshere2015_legacy
    ;;

esac

here once $setup is matched with all the entire command list is excecuted

Also, it is not restricted that you should place all in quote " "

case "$setup" in all)

        echo "runnning all setups on Vsphere-2015."
        vpshere2015_primary
        vpshere2015_M1N1
        vpshere2015_M2N1                               =======> these are methods 
        vpshere2015_legacy
    ;;

esac

will also work fine

Upvotes: 1

Related Questions