while脚本快速定义数组
[root@localhost ~]# vim while12.sh #脚本编辑
#!/bin/bash
#循环读取文件,定义数组
while read line
do
#hosts:数组名
#[++i]: 索引递增,++i是1开始,i++是0开始
#$line:值,即文件中的内容
hosts[++i]=$line
done < /etc/hosts
#输出索引每一行
for i in ${!hosts[@]}
do
echo "$i : ${hosts[$i]}"
done
[root@localhost ~]# bash while12.sh #执行脚本
1 : 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2 : ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6