![BASH 脚本:在 CASE 语句中声明一个变量以供全局使用](https://linux22.com/image/143063/BASH%20%E8%84%9A%E6%9C%AC%EF%BC%9A%E5%9C%A8%20CASE%20%E8%AF%AD%E5%8F%A5%E4%B8%AD%E5%A3%B0%E6%98%8E%E4%B8%80%E4%B8%AA%E5%8F%98%E9%87%8F%E4%BB%A5%E4%BE%9B%E5%85%A8%E5%B1%80%E4%BD%BF%E7%94%A8.png)
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
.