在shell中的case语句中获取语法错误

时间:2014-09-30 11:45:41

标签: shell case

#!/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
    ;;

我是shell的新手,在这段代码之后

bat.sh:第38行:意外令牌附近的语法错误newline' 'at.sh: line 38: ;;

我想根据用户在命令行中提供的输入运行一些函数

2 个答案:

答案 0 :(得分:1)

case声明必须以esac

结尾

case的语法是

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

所以这里应该是

case "$setup" in "all")

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

esac

一旦$setupall匹配,整个命令列表就会被删除

此外,您不应将all放在引用" "

案例“$ setup”in all)

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

esac

也可以正常使用

答案 1 :(得分:1)

您只需要关闭所有if(使用“ fi ”)和case语句(使用“ esac ”)。

我还要改变

if [ ! "vcva" ];then

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

结果如下:

#!/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