Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Shell脚本中,如何循环通过包含其他变量的变量_Shell_Loops - Fatal编程技术网

在Shell脚本中,如何循环通过包含其他变量的变量

在Shell脚本中,如何循环通过包含其他变量的变量,shell,loops,Shell,Loops,我需要创建一个循环。循环变量包含其他变量。这就是我尝试过的 parentClients="name1 name2" effectedClients="value1 value2" otherClients="something1 something2 something3" client_types="$parentClients $effectedClients $otherClients" do echo $client #this should print "parentClient

我需要创建一个循环。循环变量包含其他变量。这就是我尝试过的

parentClients="name1 name2"
effectedClients="value1 value2"
otherClients="something1 something2 something3"
client_types="$parentClients $effectedClients $otherClients"
do
   echo $client
#this should print "parentClients" in 1st iteration and "effectedClients" in second and so on.
   for ct in $client
      do
        echo $ct
#this should print name1 name2 nd so on.
      done
      echo "done with one set"
done
此代码的问题在于,它使用bash解析所有值并分配给变量client_类型

使用bash,我们可以使用数组和间接寻址:

parentClients=(name1 name2)
effectedClients=(value1 value2)
otherClients=(something1 something2 something3)
client_types=(parentClients effectedClients otherClients)
for client in "${client_types[@]}"
do
   echo "client=$client"
   c=$client[@]
   for ct in "${!c}"
   do
      echo "  ct=$ct"
   done
   echo "done with one set"
done
这将产生以下输出:

client=parentClients
  ct=name1
  ct=name2
done with one set
client=effectedClients
  ct=value1
  ct=value2
done with one set
client=otherClients
  ct=something1
  ct=something2
  ct=something3
done with one set
client=parentClients
  ct=name1
  ct=name2
done with one set
client=effectedClients
  ct=value1
  ct=value2
done with one set
client=otherClients
  ct=something1
  ct=something2
  ct=something3
done with one set
语句
parentClients=(name1 name2)
创建了一个名为
parentClients
的数组,该数组的值分别为
name1
name2
。表达式
${!c}
使用间接寻址来访问名称由
c
给出的数组

使用POSIX外壳 对于POSIX shell,我们必须使用变量而不是数组,并且使用
eval
代替间接寻址:

parentClients="name1 name2"
effectedClients="value1 value2"
otherClients="something1 something2 something3"
client_types="parentClients effectedClients otherClients"
for client in $client_types
do
   echo "client=$client"
   eval "client_list=\$$client" # Potentially dangerous step
   for ct in $client_list
   do
      echo "  ct=$ct"
   done
   echo "done with one set"
done
由于
eval
要求对数据源有一定的信任,因此应谨慎使用

这将产生以下输出:

client=parentClients
  ct=name1
  ct=name2
done with one set
client=effectedClients
  ct=value1
  ct=value2
done with one set
client=otherClients
  ct=something1
  ct=something2
  ct=something3
done with one set
client=parentClients
  ct=name1
  ct=name2
done with one set
client=effectedClients
  ct=value1
  ct=value2
done with one set
client=otherClients
  ct=something1
  ct=something2
  ct=something3
done with one set