循环遍历Bash中的字符串数组?我想编写一个循环遍历15个字符串的脚本(可能是数组吗?)这有可能吗?类似于:for databaseName in listOfNamesthen
# Do somethingend
3 回答
慕少森
TA贡献2019条经验 获得超9个赞
## declare an array variabledeclare -a arr=("element1" "element2" "element3")## now loop through the above arrayfor i in "${arr[@]}"do echo "$i" # or do whatever with individual element of the arraydone# You can access them using echo "${arr[0]}", "${arr[1]}" also
declare -a arr=("element1" "element2" "element3" "element4" )
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
#!/bin/bash## declare an array variabledeclare -a array=("one" "two" "three")# get length of an arrayarraylength=${#array[@]}# use for loop to read all values and indexesfor (( i=1; i<${arraylength}+1; i++ ));do echo $i " / " ${arraylength} " : " ${array[$i-1]}done
1 / 3 : one2 / 3 : two3 / 3 : three
添加回答
举报
0/150
提交
取消