六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 67|回复: 0

【转载】linux shell 数组建立及使用技巧

[复制链接]

升级  75.33%

43

主题

43

主题

43

主题

秀才

Rank: 2

积分
163
 楼主| 发表于 2013-2-4 13:36:01 | 显示全部楼层 |阅读模式
详解请见:http://suo.iteye.com/blog/1118825


从上面讲到的,大家可以发现linux shell 的数组已经很强大了,常见的操作已经绰绰有余了。

shell 按行读取并保存成数组



从ip.txt里读取IP.然后把IP地址赋值到一个数组里.
IP文件如下:
Address:  220.181.26.163
Address:  220.181.26.174
Address:  220.181.26.175
Address:  220.181.26.176
Address:  220.181.19.228
Address:  220.181.19.229
Address:  220.181.26.161
Address:  220.181.26.162



方法一:
for x in ` awk '{print $2}' ip.txt `
{
echo $x
}
方法二:
ARRAY=($(awk '{print $2}' ip.txt))
方法三:
n=0;while read a b;do array[$n]=$b;((n++));done<ip.txt

方法四:
n=1
while ((n<=$(cat ip.txt|wc -l)))
do
    ipaddr[$n]=$(cat ip.txt|sed -n "${n}p"|awk '{print $2}')
    ((n+=1))
done
n=`expr $n - 1`
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表