shell中一维数组值得获取
(1)数组的定义
root@tcx4440-03:~# a=(1 2 3 4)
root@tcx4440-03:~# echo ${a[1]}
2
root@tcx4440-03:~# a[0]=1
root@tcx4440-03:~# a[1]=2
root@tcx4440-03:~# echo ${a[0]}
1
(2)数组值得获取
root@tcx4440-03:~# var[0]=1
root@tcx4440-03:~# var[1]=2
root@tcx4440-03:~# echo $var[1]
1[1]
root@tcx4440-03:~# echo ${var[1]}
2
(3)获取数组的长度。用${#数组名[@或*]} 可以得到数组长度
root@tcx4440-03:~# a=(10 20 30 40 50 60)
root@tcx4440-03:~# echo ${#a[*]}
6
root@tcx4440-03:~# echo ${#a[@]}
6
root@tcx4440-03:~# echo ${a[@]}
10 20 30 40 50 60
root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60
(4)数组的读取,用${数组名[下标]} 下标是从0开始 下标是:*或者@ 得到整个数组内容
root@tcx4440-03:~# echo ${a[1]}
20
root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60
(5)数组的赋值
root@tcx4440-03:~# a[3]=100
root@tcx4440-03:~# echo ${a[*]}
10 20 30 100 50 60
(6)数组的删除,直接通过:unset 数组[下标] 可以清除相应的元素,不带下标,清除整个数据。
root@tcx4440-03:~# echo ${a[*]}
10 20 30 100 50 60
root@tcx4440-03:~# unset a[1]
root@tcx4440-03:~# echo ${#a[@]}
5
root@tcx4440-03:~# echo ${a[*]}
root@tcx4440-03:~# unset a
root@tcx4440-03:~# echo ${#a[*]}
0
(7)分片使用,直接通过 ${数组名[@或*]:起始位置:长度} 切片原先数组,返回是字符串,中间用“空格”分开,因此如果加上”()”,将得到切片数组,上面例子:c 就是一个新数据。
A:tt只是一个变量,不是一个数组,所以一定要加啊()
root@tcx4440-03:~# tt=${a[*]:2:4}
root@tcx4440-03:~# echo $tt 30 40 50 60
root@tcx4440-03:~# echo ${tt[1]}
root@tcx4440-03:~# echo ${tt[2]}
B: c仍然是一个数组
root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60
root@tcx4440-03:~# c=(${a[*]:1:4})
root@tcx4440-03:~# echo ${c[*]}
20 30 40 50
root@tcx4440-03:~# echo $c
20
root@tcx4440-03:~# echo ${c[2]}
40
root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60
(8)替换,调用方法是:${数组名[@或*]/查找字符/替换字符} 该操作不会改变原先数组内容,如果需要修改,可以看上面例子,重新定义数据。
root@tcx4440-03:~# echo ${a[*]}
10 20 30 40 50 60
root@tcx4440-03:~# echo ${a[*]/60/100}
10 20 30 40 50 100
root@tcx4440-03:~# echo ${a[*]} //数组a的值是不变的
10 20 30 40 50 60
root@tcx4440-03:~# tt=${a[*]/100/1000} //没有括号,则是一个变量,不是数组
root@tcx4440-03:~# echo $tt
10 20 30 40 50 60
root@tcx4440-03:~# echo ${tt[1]}
root@tcx4440-03:~# tt=(${a[*]/60/1000}) //有括号,则tt是一个数组
root@tcx4440-03:~# echo ${tt[5]}
1000
root@tcx4440-03:~# echo ${tt[*]}
10 20 30 40 50 1000
root@tcx4440-03:~# echo ${a[*]} //数组a是没有变化的
10 20 30 40 50 60
shell中一维数组值得获取的更多相关文章
- Shell中的数组及其相关操作
http://blog.csdn.net/jerry_1126/article/details/52027539 Shell中数据类型不多,比如说字符串,数字类型,数组.数组是其中比较重要的一种,其重 ...
- shell中的函数、shell中的数组、告警系统需求分析
7月16日任务 20.16/20.17 shell中的函数20.18 shell中的数组20.19 告警系统需求分析 20.16/20.17 shell中的函数 函数就是一个子shell就是一个代码段 ...
- linux shell 中的数组的取值 遍历 替换 删除操作
引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...
- Linux centosVMware shell中的函数、shell中的数组、
一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function _name() { command ...
- 九 Shell中的数组
数组:用一个变量存储一组数据,并能够对这组数据中的某一个数据单独操作. 数组的类型:一维数组.二维数组.多维数组 变量的类型 Shell中默认无类型 变量的值默认均视为文本 用在数字运算中时,自动将其 ...
- C语言中一维数组
(1)输出数组元素 #include<stdio.h> int main() { int index; /*定义循环变量*/ int iArray[6]={0,1,2,3,4,5}; /* ...
- shell中的数组
在shell脚本中,除了通常使用的shell变量外,有时也需要复杂的数据结构去实现一些功能,这里简单说明一下shell数组的使用方法: 初始化方法 _array_name[0]="rando ...
- JavaScript 实现彩票中随机数组的获取
1.效果图: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- Linux Shell中的数组及遍历 转
转自:http://www.linuxidc.com/Linux/2011-09/42929.htm 在Linux下使用shell的时候,为方便起见,偶尔会用到一下数组.数组的申明方式是: array ...
随机推荐
- UVALive 6450 Social Advertising DFS解法
题意:一些人有朋友关系,在某个人的社交网站上投放广告可以被所有该人的直接朋友看到,问最小投放多少个广告使给出的人都看到广告.(n<=20) 解法:看到n的范围可以想到用二进制数表示每个人被覆盖与 ...
- 三维网格形变算法(Laplacian-Based Deformation)
网格上顶点的Laplace坐标(均匀权重)定义为:,其中di为顶点vi的1环邻域顶点数. 网格Laplace坐标可以用矩阵形式表示:△=LV,其中,那么根据网格的Laplace坐标通过求解稀疏线性方程 ...
- 搜索服务Solr集群搭建 使用ZooKeeper作为代理层
上篇文章搭建了zookeeper集群 那好,今天就可以搭建solr搜服服务的集群了,这个和redis 集群不同,是需要zk管理的,作为一个代理层 安装四个tomcat,修改其端口号不能冲突.8080~ ...
- [cb]ScriptableObject 序列化
ScriptableObject ScriptableObject是一个类,它允许你存储大量用于共享的数据独立脚本实例,不要迷惑这个类同样可以叫做 SerializableObject,可以理解成是一 ...
- Android开发配置,消除SDK更新时的“https://dl-ssl.google.com refused”异常
消除SDK更新时的“https://dl-ssl.google.com refused”错误 消除SDK更新时,有可能会出现这样的错误:Download interrupted: hostname i ...
- 安装StarUML 及使用时序图(Sequence Diagram)和用例图(use case diagram)
时序图 用例图
- BIO、NIO与NIO.2的区别与联系
BIO.NIO.NIO.2之间的区别主要是通过同步/异步.阻塞/非阻塞来进行区分的 同步: 程序与操作系统进行交互的时候采取的是问答的形式 异步: 程序与操作系统取得连接后,操作系统会主动通知程序消息 ...
- Spring之AOP
package org.zln.module.test3_aop.interceptor; import org.aspectj.lang.ProceedingJoinPoint; import or ...
- [转]hadoop hdfs常用命令
FROM : http://www.2cto.com/database/201303/198460.html hadoop hdfs常用命令 hadoop常用命令: hadoop fs 查看H ...
- 推荐一款开源的C#TCP通讯框架
原来收费的TCP通讯框架开源了,这是一款国外的开源TCP通信框架,使用了一段时间,感觉不错,介绍给大家 框架名称是networkcomms 作者开发了5年多,目前已经停止开发,对于中小型的应用场景,够 ...