BASH 脚本:在 CASE 语句中声明一个变量以供全局使用

BASH 脚本:在 CASE 语句中声明一个变量以供全局使用
case $location in
    ColocationOne) 
        # Define MOSES LAKE workerarray
        server[0]=serverone
        server[1]=servertwo
esac

echo ${server[0]}

我如何让它回显:serverone

答案1

您设置location为字符串ColocationOne

#!/bin/bash

location=ColocationOne

case $location in
    ColocationOne) 
        # Define MOSES LAKE workerarray
        server[0]=serverone
        server[1]=servertwo
        # or:  server=( serverone servertwo )
esac

echo "${server[0]}"

该脚本将打印serverone.

相关内容