浅谈shell数组的定义及循环
shell中数组的定义及遍历,先直接看示例:
#!/bin/sh
#定义方法一 数组定义为空格分割
arrayWen=(a b c d e f)
#定义方法二
arrayXue[0]="m"
arrayXue[1]="n"
arrayXue[2]="o"
arrayXue[3]="p"
arrayXue[4]="q"
arrayXue[5]="r"
#打印数组长度
echo ${#arrayWen[@]}
#for 循环遍历
for var in ${arrayWen[@]};
do
echo $var
done
#while循环遍历
i=0
while [[ i -lt ${#arrayXue[@]} ]]; do
echo ${arrayXue[i]}
let i++
done执行结果如下:

接下来进行下说明
数组定义可以有两种方式
方式一
#定义方法一 数组定义为空格分割 arrayWen=(a b c d e f)
方式二
#定义方法二 arrayXue[0]="m" arrayXue[1]="n" arrayXue[2]="o" arrayXue[3]="p" arrayXue[4]="q" arrayXue[5]="r"
如同java的数组一般,无法定义未知长度的数组,必须得指定长度,无论是通过下标还是直接通过元素。
获取数组长度
${#arrayWen[@]} 相关推荐
applecarelte 2020-06-04
SciRui 2019-12-02
rechanel 2018-04-19
tterminator 2018-04-19
lianshaohua 2016-02-18
alwayshelloworld 2010-05-16
xiaonamylove 2020-08-16
Jieen 2020-05-18
赵家小少爷 2020-01-04
赵家小少爷 2019-12-07
酷云的csdn 2019-11-08
striver0 2011-02-12
静湖微风 2012-05-24
laisean 2020-11-11
liuyh 2020-08-09
Zaratustra 2020-06-26
kehanxin 2020-05-26