我正在编写一个 bash 脚本来在大约 20 个不同的服务器上使用 rsync 和更新文件。
我已经弄清楚了 rsync 部分。我遇到的问题是通过变量列表。
到目前为止,我的脚本如下所示:
#!/bin/bash
SERVER1="192.xxx.xxx.2"
SERVER2="192.xxx.xxx.3"
SERVER3="192.xxx.xxx.4"
SERVER4="192.xxx.xxx.5"
SERVER5="192.xxx.xxx.6"
SERVER6="192.xxx.xxx.7"
for ((i=1; i<7; i++))
do
echo [Server IP Address]
done
Run Code Online (Sandbox Code Playgroud)
哪里[Server IP Address]
应该是关联变量的值。因此,当 i = 1 时,我应该回显 $SERVER1 的值。
我已经尝试了几次迭代,包括
echo "$SERVER$i" # printed the value of i
echo "SERVER$i" # printer "SERVER" plus the value of i ex: SERVER 1 where i = 1
echo $("SERVER$i") # produced an error SERVER1: command not found where i = 1
echo …
Run Code Online (Sandbox Code Playgroud)