通过bash脚本字符串数组循环? [英] Loop through array of strings in bash script?

查看:109
本文介绍了通过bash脚本字符串数组循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写通过15串循环脚本(​​可能是数组?)是可能的吗?

是这样的:

 将databaseName在listOfNames
然后
# 做一点事
结束


解决方案

您可以使用它是这样的:

  ##声明数组变量
声明-a ARR =(中element1在element2,元素3)##现在通过上面的循环数组
因为我在$ {ARR [@]}

   回声$ I
   #或做任何与数组的单个元素
DONE#你可以用echo$ {ARR [0]},$ {改编[1]}还访问它们

I want to write a script that loops through 15 strings (array possibly?) is that possible?

Something like:

for databaseName in listOfNames
then
# do something
end 

解决方案

You can use it like this:

## 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

这篇关于通过bash脚本字符串数组循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆