shell编程-数组

shell 编程-数组

什么是数组?
数组也是一种变量,常规变量只能保存一个值,数组可以保存多个值

#普通数组:只能用整数作为数组的索引--0  下标
#关联数组:可以使用字符串作为数组的索引
数组定义
普通数组定义:
[root@linux-server script]# books=( linux shell awk sed ) ---在python中叫列表

引用:${array_name[index]} #引用 

[root@linux-server script]# echo ${books[0]}
linux
[root@linux-server script]# echo ${books[1]}
shell
[root@linux-server script]# echo ${books[2]}
awk

#关联数组需要提前声明
Declare命令:
[test @test test]# declare [-选项]
参数说明:
-a :#定义为数组--array
-A : #定义关联数组

例1
declare -A myarry1
[root@linux-server script]# declare -A myarry1
[root@linux-server script]# myarry1=([name]=soso666 [sex]=man [age]=18)
[root@linux-server script]# echo ${myarry1[name]}
soso666
[root@linux-server script]# echo ${myarry1[age]}
18
定义方法1:
[root@linux-server script]# declare -a myarry=(5 6 7 8)
[root@linux-server script]# echo ${myarry[2]}
显示结果为 7

定义方法2:
    # array=( one two three four five six )
    # array2=(tom jack alice)
    # array3=(`cat /etc/passwd`) #希望是将文件中的每一行作为一个值赋给数组array3
    # array4=(tom jack alice "bash shell")
    # array5=(1 2 3 4 5 6 7 "linux shell" [20]=saltstack)

定义方法3: 
#语法:数组名[index]=变量值
#!/bin/bash 
area[11]=23 
area[13]=37 
area[51]="UFO"

示例
[root@linux-server script]# vim shuzu.sh
#!/bin/bash
NAME[0]="BJ"
NAME[1]="SH"
NAME[2]="SZ"
NAME[3]="GZ"
NAME[4]="HZ"
NAME[5]="ZZ"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"
echo "sixth Index: ${NAME[5]}"

输出结果
[root@linux-server script]# bash shuzu.sh 
First Index: BJ
Second Index: SH
sixth Index: ZZ
访问数组

当设置任何数组变量时,可以访问它

[root@linux-server script]# aa=(haha heihei baibai)
[root@linux-server script]# echo ${aa[0]}   #访问数组中的第一个元素
[root@linux-server script]# echo ${aa[@]}   #访问数组中所有的元素 等同与echo ${aa[*]} 
[root@linux-server script]# echo ${#aa[@]}  #统计元素的个数 
[root@linux-server script]# echo ${!aa[@]}  #打印索引

您可以访问数组中的所有项目通过以下方式之一:

${array_name[*]}
${array_name[@]}

示例

[root@linux-server script]# vim shuzu1.sh
#!/bin/sh
NAME[0]="BJ"
NAME[1]="SH"
NAME[2]="SZ"
NAME[3]="GZ"
NAME[4]="HZ"
echo "${NAME[*]}"
echo "${NAME[@]}"

输出结果

[root@linux-server script]# bash shuzu1.sh 
BJ SH SZ GZ HZ
BJ SH SZ GZ HZ

疑难点
shell数组中"*" 和 “@” 区别

关于在shell脚本中数组变量中 “*”跟 “@” 区别
*当变量加上"" 会当成一串字符串处理. 
@当变量加上"" 依然当做数组处理. 
在没有加上"" 的情况下效果是等效的.

示例

[root@linux-server script]# vim test1.sh
#!/usr/bin/env bash
array=(gz cloud 19)
echo "case 1"
for line in "${array[@]}"
do
		echo $line
done

echo "case 2"
for line in "${array[*]}"
do
		echo $line
done

echo "case 3"
for line in ${array[*]}
do
		echo $line
done

echo "case 4"
for line in ${array[@]}
do
		echo $line
done

执行结果

[root@linux-server script]# bash test1.sh 
case 1
gz
cloud
19
case 2
gz cloud 19
case 3
gz
cloud
19
case 4
gz
cloud
19
遍历数组---while---处理行比较擅长,不管有没有空格都是一行
将一个文件中的每一行作为数组的元素赋值给数组并遍历
[root@newrain array]# cat array01.sh 
#!/bin/bash
#++i:是先自加1后赋值;i++:是先赋值后自加1。 i不定义默认值0
while read line   #数组赋值
do
        host[i++]=$line
done </etc/hosts
echo
for i in ${!host[@]}   #数组遍历,建议使用索引遍历
do
        echo "$i:${host[i]}"
done

遍历数组for
[root@newrain array]# cat array02.sh 
#!/bin/bash
#IFS=$'\n'
for line in `cat /etc/hosts`
do
        host[j++]=$line
done
for  i in ${!host[@]}
do
        echo ${host[i]}
done 

#注意:for循环中会将tab\空格\回车作为分隔符默认为空格.

扩展:
 Shell 脚本中有个变量叫IFS(Internal Field Seprator) ,内部域分隔符。IFS是一个变量,当shell处理"命令替换""参数替换"时,shell根据IFS的值,默认是space, tab, newline来拆解读入的变量,然后对特殊字符进行处理,最后重新组合赋值给该变量。
使用方式:

OLD_IFS=$IFS #先保存原始值
IFS="\n"  #改变IFS的值
...
...
IFS=$OLD_IFS #如果用的时候在还原IFS的原始值

[root@localhost ~]# vim count_shells.sh
#!/usr/bin/bash
declare -A shells
while read line
do
        type=`echo $line | awk -F":" '{print $NF}'`
        let shells[$type]++   #匹配到之后加1
done < /etc/passwd
for i in ${!shells[@]}
do
        echo "$i: ${shells[$i]}"  #数组名加上索引统计出值
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值