这可能很难解释,但我还是想试一试。此代码片段在 bash 5.0.16 中有效,但在 4.2.46 中无效
我有一个数组 basicHosts=(host1 host2 host3),这是一个动态生成的数组,名称随时可能更改。我已使用此数组创建 3 个变量,其名称是 basicHosts 中的值。现在我有这个:
host1=(Name CPU Memory IP)
host2=(Name CPU Memory IP)
host3=(Name CPU Memory IP)
我想要做的是循环遍历 basicHosts 中定义的每个变量并打印出值。下面是我在 bash 5 中尝试过的方法
#At this point each host should have its own matrix
#Now we need to set values for each host
echo "Here is what the hostname matrix looks like"
for host in "${basicHosts[@]}"
do
temp=$host
for attr in "${temp[@]}"
do
echo $attr
done
done
我是 Bash 新手,请原谅我的格式不好。只是想边做边学。希望你们能给我一些建议。
编辑:完整代码
#!/bin/bash
clear
hostSet="True"
defaultSet="False"
declare -a basicHosts
basicHosts=(host1 host2 host3)
clear
echo "Hello, below are the default hosts that will be created
"
declare -i i=0
for hosts in "${basicHosts[@]}"
do
echo "$i: $hosts"
i=$i+1
done
echo "Hosts are Set"
#Going to start creating the arrays based off of the host names
for host in "${basicHosts[@]}"
do
declare -a $host
eval "$host=(Name CPU Memory)"
done
echo "Variable matrix created"
#At this point each host should have its own matrix
#Now we need to set values for each host
echo "Here is what the hostname matrix looks like"
for host in "${basicHosts[@]}"
do
typeset temp="$host"
echo $host
for attribute in "${temp[@]}"
do
echo "${host[$attribute]}"
done
done
答案1
这里的问题在于将 host1、host2 和 host3 声明为数组并不是变量到基本主机数组并使用级联,这是解决该问题的另一种方法:
#!/usr/bin/bash
#At this point each host should have its own matrix
#Now we need to set values for each host
echo "Here is what the hostname matrix looks like"
declare -a basicHosts
## allhosts array here contains n hosts
for host in allhosts ; do
# You can make any treatment here
basicHosts+=( ${host[*]} )
done
for host in "${basicHosts[@]}" ; do
for temp in "${host[@]}" ; do
echo "${host[$temp]}"
done
done
编辑 :
自上次编辑以来,每个主机都应该是可迭代的,并且其上的每个值(名称、CPU、内存)也应该是可迭代的。这可以使用关联数组 + 字符串到数组的转换来实现,以下是您要使用的代码的更新。
#!/bin/bash
clear
hostSet="True"
defaultSet="False"
declare -a basicHosts
basicHosts=(host1 host2 host3)
clear
echo "Hello, below are the default hosts that will be created
"
declare -i i=0
for hosts in "${basicHosts[@]}"
do
echo "$i: $hosts"
i=$i+1
done
echo "Hosts are Set"
#Going to start creating the arrays based off of the host names
declare -A results
for host in "${basicHosts[@]}"
do
values="Name CPU Memory"
arrayvalues=(${values})
results+=( ["$host"]="${arrayvalues[@]}")
done
echo "Variable matrix created"
#At this point each host should have its own matrix
#Now we need to set values for each host
echo "Here is what the hostname matrix looks like"
echo "---------------------------------"
array=(${results[${host}]})
for host in "${!results[@]}" ; do
echo -ne "${host} : "
for value in "${array[@]}" ;do
echo -ne "$value - "
done
echo -e "\n"
done
echo "---------------------------------"
输出应如下所示:
Here is what the hostname matrix looks like
---------------------------------
host2 : Name - CPU - Memory -
host3 : Name - CPU - Memory -
host1 : Name - CPU - Memory -
---------------------------------