循环Bash中的字符串数组?
我想写一个脚本,通过15个字符串循环(数组可能?)这可能吗?
就像是:
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 a b c d e f; 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
链接地址: http://www.djcxy.com/p/4959.html