通过在Bash中的string数组循环?

我想写一个脚本,通过15个string循环(数组可能?)这是可能的吗?

就像是:

for databaseName in listOfNames then # Do something end 

你可以像这样使用它:

 ## declare an array variable declare -a arr=("element1" "element2" "element3") ## now loop through the above array for i in "${arr[@]}" do echo "$i" # or do whatever with individual element of the array done # You can access them using echo "${arr[0]}", "${arr[1]}" also 

也适用于多行数组声明

 declare -a arr=("element1" "element2" "element3" "element4" ) 

当然,这是可能的。

 for databaseName in abcdef; do # do something like: echo $databaseName done 

有关详细信息,请参阅Bash循环 。

这些答案都不包括柜台…

 #!/bin/bash ## declare an array variable declare -a array=("one" "two" "three") # get length of an array arraylength=${#array[@]} # use for loop to read all values and indexes for (( i=1; i<${arraylength}+1; i++ )); do echo $i " / " ${arraylength} " : " ${array[$i-1]} done 

输出:

 1 / 3 : one 2 / 3 : two 3 / 3 : three 

与4ndrew的回答一样:

 listOfNames="RA RB RC RD" # To allow for other whitespace in the string: # 1. add double quotes around the list variable, or # 2. see the IFS note (under 'Side Notes') for databaseName in "$listOfNames" # <-- Note: Added "" quotes. do echo "$databaseName" # (ie do action / processing of $databaseName here...) done # Outputs # RA # RB # RC # RD 

B.名字中没有空格:

 listOfNames="RA RB RC RD" for databaseName in $listOfNames # Note: No quotes do echo "$databaseName" # (ie do action / processing of $databaseName here...) done # Outputs # RA # RB # R # C # RD 

笔记

  1. 在第二个例子中,使用listOfNames="RA RB RC RD"具有相同的输出。

其他引入数据的方法包括:

  • stdin(下面列出),
  • variables ,
  • 一个数组 (接受的答案),
  • 档案 …

从标准input读取

 # line delimited (each databaseName is stored on a line) while read databaseName do echo "$databaseName" # ie do action / processing of $databaseName here... done # <<< or_another_input_method_here 
  1. 可以在脚本中指定bash IFS “字段分隔符到行”[ 1 ]分隔符,以允许其他空格(即IFS='\n' ,或MacOS IFS='\r' )。
  2. 我也喜欢接受的答案:) – 我已经包括这些片段作为其他有用的方式,也回答了这个问题。
  3. 在脚本文件的顶部包含#!/bin/bash表示执行环境。
  4. 花了我几个月才弄清楚如何编写这个简单:)

其他来源( while read loop )

您可以使用${arrayName[@]}的语法

 #!/bin/bash # declare an array called files, that contains 3 values files=( "/etc/passwd" "/etc/group" "/etc/hosts" ) for i in "${files[@]}" do echo "$i" done 

这也很容易阅读:

 FilePath=( "/tmp/path1/" #FilePath[0] "/tmp/path2/" #FilePath[1] ) #Loop for Path in "${FilePath[@]}" do echo "$Path" done 

声明数组不适用于Korn shell。 对于Korn shell使用下面的例子:

 promote_sla_chk_lst="cdi xlob" set -A promote_arry $promote_sla_chk_lst for i in ${promote_arry[*]}; do echo $i done 

尝试这个。 它正在工作和testing。

 for k in "${array[@]}" do echo $k done # For accessing with the echo command: echo ${array[0]}, ${array[1]} 

如果你正在使用Korn shell,有“ set -A databaseName ”,否则有“ declare -a databaseName

要编写一个在所有shell上工作的脚本,

  set -A databaseName=("db1" "db2" ....) || declare -a databaseName=("db1" "db2" ....) # now loop for dbname in "${arr[@]}" do echo "$dbname" # or whatever done 

它应该是所有炮弹的工作。

 listOfNames="db_one db_two db_three" for databaseName in $listOfNames do echo $databaseName done 

要不就

 for databaseName in db_one db_two db_three do echo $databaseName done 

单线循环,

  declare -a listOfNames=('db_a' 'db_b' 'db_c') for databaseName in ${listOfNames[@]}; do echo $databaseName; done; 

你会得到这样的输出,

 db_a db_b db_c 
 List=(Item1 Item2 Item3 Item4 Item5 Item6) for Item in ${List[@]} do echo $Item done 

OUT PUT:

  Item1 Item2 Item3 Item4 Item5 Item6 

@

 List=( Item1 Item2 Item3 Item4 Item5 Item6 'Item 7 needs to be quoted to preserve spaces' ) for Item in "${List[@]}" do echo $Item done 

OUT PUT:

 Item1 Item2 Item3 Item4 Item5 Item6 Item 7 needs to be quoted to preserve spaces 

*

 List=( Item1 Item2 Item3 Item4 Item5 Item6 'Item 7 needs to be quoted to preserve spaces' ) for Item in "${List[*]}" do echo $Item echo $'\n' done 

OUT PUT使用*而不是@产生:

 Item1 Item2 Item3 Item4 Item5 Item6 Item 7 needs to be quoted to preserve spaces 

 List=() for Item in `seq 1 1 16` do List+=(Item$Item) done for Item in ${List[*]} do echo $Item done 

输出是:

 Item1 Item2 Item3 Item4 Item5 Item6 Item7 Item8 Item9 Item10 Item11 Item12 Item13 Item14 Item15 Item16 

这与user2533809的答案类似,但每个文件将作为单独的命令执行。

 #!/bin/bash names="RA RB RC RD" while read -r line; do echo line: "$line" done <<< "$names" 

每个Bash脚本/会话可能的第一行:

 say() { for line in "${@}" ; do printf "%s\n" "${line}" ; done ; } 

使用例如:

 $ aa=( 7 -4 -e ) ; say "${aa[@]}" 7 -4 -e 

可以考虑: echo在这里作为选项解释

我通过一个我的项目的数组循环git pull更新:

 #!/bin/sh projects=" web ios android " for project in $projects do cd $HOME/develop/$project && git pull end