SHELL脚本--shell数组基础
bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html
数组和变量的区别是:变量在内存中占用的空间是离散的,数组在内存中是先开辟一段连续的大内存空间,随后数组中的每个元素都放入数组内存中。数组元素使用数组index标识。
bash里有两种数组:普通数组和关联数组。普通数组只能使用整型数值作为数组索引,关联数组可以使用字符串作为索引。所谓的关联数组,它的另外三种称呼:字典(dict)、hash结构和映射(map),是一种key和value一 一对应的关系。
1.9.1 普通数组
定义数组的方式一:
[root@xuexi tmp]# array_test=( )
它们分别存储在索引位0-3的位置上,是array_test[0]到array_test[3]对应的值。此时array_test[0]表示的是一个变量,所以使用$来引用。数组的引用方式:${array_name[index]}。
[root@xuexi tmp]# echo ${array_test[]}
注意数组中定义是使用空格作为分隔符定义在括号内,而不是逗号。如果使用逗号,则它们将作为一个整体,也就是数组索引0的值。如果使用逗号,则:
[root@xuexi tmp]# array_test=(,,,)
[root@xuexi tmp]# echo ${array_test[]} # 整体结果作为索引0位的值。
,,,
定义数组的方式二:可以自定义索引位。
[root@xuexi tmp]# array_test1[]=
[root@xuexi tmp]# array_test1[]=
[root@xuexi tmp]# array_test1[]=
[root@xuexi tmp]# array_test1[]=
[root@xuexi tmp]# echo ${array_test1[*]}
但是在索引位4之后定义array_test1[7]=7则表示5和6的数组变量未定义,即不存在,这可以通过统计变量的元素个数来验证。但在shell中是可以直接引用未定义变量的,只不过它们的初始值是空或是0。
(1).打印数组所有值。
[root@xuexi tmp]# echo ${array_test1[*]}
或者使用@符号。
[root@xuexi tmp]# echo ${array_test1[@]}
(2).查看数组索引号。
[root@xuexi tmp]# echo ${!array_test1[*]}
或者
[root@xuexi tmp]# echo ${!array_test1[@]}
(3).数组中变量长度和数组长度。
[root@xuexi tmp]# echo ${#array_test1[]} # 显示下标为1的数组变量的字符长度
[root@xuexi tmp]# echo ${#array_test1[*]} # 显示数组中的元素个数(只统计值不为空的元素)
[root@xuexi tmp]# echo ${#array_test1[@]} # 显示数组中的元素个数(只统计值不为空的元素)
1.9.2 关联数组
关联数组支持字符串作为数组索引。使用关联数组必须先使用declare -A声明它。
[root@xuexi tmp]# declare -A array_dep # 声明之后就可以给其赋值了 [root@xuexi tmp]# array_dep=([name1]=longshuai [name2]=xiaofang)
其中name1和name2就是关联数组的index。引用数组变量时需要使用index来引用对应的值。
[root@xuexi tmp]# echo ${array_dep[name1]}
longshuai
也可以分开赋值。
[root@xuexi tmp]# array_dep[name3]=zhangsan
[root@xuexi tmp]# array_dep[name4]=lisi
[root@xuexi tmp]# echo ${array_dep[name4]}
lisi
(1).查看数组所有值。
[root@xuexi tmp]# echo ${array_dep[*]}
zhangsan xiaofang longshuai lisi # 可以看到是字母倒序排列的
或者:
[root@xuexi tmp]# echo ${array_dep[@]}
zhangsan xiaofang longshuai lisi
(2).查看数组索引号。
[root@xuexi tmp]# echo ${!array_dep[@]} # 对应数组值的倒序排列
name3 name2 name1 name4
或者:
[root@xuexi tmp]# echo ${!array_dep[*]}
name3 name2 name1 name4
(3).统计数组长度。
[root@xuexi tmp]# echo ${#array_dep[*]}
或者:
[root@xuexi tmp]# echo ${#array_dep[@]}
1.9.3 数组元素截取、替换
和变量的截取和替换是类似的。
array=( )
array0=${array[*]::} # 从数组全部元素中第2个元素向后截取2个元素出来(即3 )
array1=${array[*]//} # 将数组中的5替换称6
还有从左匹配删除从右匹配删除,和变量是一样的。
array=(one two three foue five)
array1=${array[*]#*o} # 从左非贪婪匹配并删除所有数组变量中匹配内容
array2=${array[*]##*o} # 从左贪婪匹配并删除所有数组变量中匹配的内容
array2=${array[*]%o} # 从右非贪婪匹配并删除所有数组变量中匹配内容
array2=${array[*]%%o} # 从右贪婪匹配并删除所有数组变量中匹配内容
1.9.4 for循环遍历数组
在shell中的循环结构中,可以使用数组名来表示整个数组变量。
for i in ${array[*]};do
echo $i
done
或者让i变成数组index的方法:
for i in ${!array[*]};do
echo ${array[$i]}
done
以下是遍历数组的三个常见用法总结:
array=($(ls /boot))
for i in ${array[*]};do # 以数组值的方式直接遍历数组
echo $i
done
for ((i=;i<${#array[*]};i++));do # 以数组变量个数的方式遍历数组
echo ${array[$i]}
done
for i in ${!array[*]};do # 以数组index的方式遍历数组
echo ${array[$i]}
done
以下是一个数组遍历的示例:统计文件中重复行的次数。假设a.log文件中内容如下。
[root@xuexi ~]# cat a.log
portmapper
portmapper
portmapper
portmapper
portmapper
portmapper
status
status
mountd
mountd
mountd
mountd
mountd
mountd
nfs
nfs
nfs_acl
nfs
nfs
nfs_acl
nlockmgr
nlockmgr
nlockmgr
nlockmgr
nlockmgr
nlockmgr
以下是数组遍历的脚本。
#!/bin/bash declare -A array_test # 定义关联数组
for i in `cat ~/a.log`
do
let ++array_test[$i]
done for j in ${!array_test[*]}
do
printf "%-15s %3s\n" $j :${array_test[$j]}
done
该脚本中第一个for循环中,以文件中内容做数组的index,每遍历到一个index就对该index进行数学运算的自加1操作。这一过程是遍历文件内容并为数组变量赋值的过程。
这样就得到如下结果:
array_test[status]=2
array_test[nfs]=4
array_test[portmapper]=6
array_test[nlockmgr]=6
array_test[nfs_acl]=2
array_test[mountd]=6
第二个for循环是将array_test数组中所有数组变量及其值取出来,这一步是遍历数组的过程。所以得到最终的结果:
[root@xuexi ~]# ./a.sh
status :
nfs :
portmapper :
nlockmgr :
nfs_acl :
mountd :
SHELL脚本--shell数组基础的更多相关文章
- 学习 shell脚本之前的基础知识
转载自:http://www.92csz.com/study/linux/12.htm 学习 shell脚本之前的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写sh ...
- 转载:shell脚本之前的基础知识
转载地址:http://www.92csz.com/study/linux/12.htm 第十二章 学习 shell脚本之前的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果 ...
- 大数据系列博客之 --- 深入简出 Shell 脚本语言(基础篇)
首先声明,此系列shell系列博客分为四篇发布,分别是: 基础篇:https://www.cnblogs.com/lsy131479/p/9914747.html 提升篇:https://www.cn ...
- 学习shell脚本之前的基础知识
日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写shell脚本,那么你就不算一个合格的管理员.目前很多单位在招聘linux系统管理员时,shell脚本的编写是必考的项目.有的单位 ...
- shell脚本之前的基础知识
日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写shell脚本,那么你就不算一个合格的管理员.目前很多单位在招聘linux系统管理员时,shell脚本的编写是必考的项目.有的单位 ...
- shell脚本之编程基础介绍
1.shell脚本简介 1.1 shell是什么? shell是一个命令解释器,它在操作系统的最外层负责直接与用户对话,把用户的输入解释给操作系统:并处理各种各样的操作系统的输入,将结果输出到屏幕返回 ...
- 第十二章 学习 shell脚本之前的基础知识
http://www.92csz.com/study/linux/12.htm [什么是shell] 简单点理解,就是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具.实际上,在shell和 ...
- 【shell脚本】 变量基础学习整理
1.linux系统环境 echo 'echo /etc/profile ' >> /etc/profile echo 'echo /etc/bashrc' >> /etc/ba ...
- Shell脚本学习-数组
跟着RUNOOB网站的教程学习的笔记 Shell数组 数组中可以存放多个值,Bash Shell只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与PHP类似). 与大部分编程语言类似,数 ...
随机推荐
- 20155205 郝博雅 Exp9 Web安全基础
20155205 郝博雅 Exp9 Web安全基础 一.实验内容 一共做了13个题目. 1.WebGoat 输入java -jar webgoat-container-7.1-exec.jar 在浏览 ...
- CSS3新增特性及知识学习线路
- Catalog
Java SE EE| Hibernate | Struts2Spring/SpringMVC | MyBatis C# Python PHP C/C++ | STL 汇编语言 ...
- HTML5中input标签有用的新属性
HTML5对input增加了一些新标签,个人觉得比较常用有效的以下几个 placeholder=“请输入” 常见用于默认提示 autofocus 自动聚焦到当前输入框 maxlength=" ...
- 大众点评selfxss结合两个csrf变废为宝(已修复,故公开,不涉及真实参数)
大众点评selfxss结合两个csrf变废为宝 漏洞不值钱,但还是蛮好玩的 漏洞信息 类型:存储型xss 场景:收藏商户后,去已收藏的商户列表可以给指定商户添加tag(与下文html标签区别) 漏洞限 ...
- gogs 安装
docker 安装gogs 准备工作 安装一个mysql数据库,创建一个数据库 gogs,字符集为utf-8 查找gogs 镜像 docker search gogs 拉取镜像到本地 docker p ...
- 编码符_new88
begin#239B38F58D59E401465E1FEE0AFA7AE2DD920EB6645F4A2075C7ABBBE2141B925668C9D635D90DE884907F4E52F921 ...
- Java 并发编程:核心理论
并发编程是Java程序员最重要的技能之一,也是最难掌握的一种技能.它要求编程者对计算机最底层的运作原理有深刻的理解,同时要求编程者逻辑清晰.思维缜密,这样才能写出高效.安全.可靠的多线程并发程序.本系 ...
- 在使用可变数组过程中遇到*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'问题
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFD ...
- [转] KVM scalability and consolidation ratio: cache none vs cache writeback
http://www.ilsistemista.net/index.php/virtualization/43-kvm-scalability-and-consolidation-ratio-cach ...