转载-Linux Shell 数组建立及使用技巧
转载自:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html
如侵犯版权,请联系我删除
linux shell在编程方面比windows 批处理强大太多,无论是在循环、运算。已经数据类型方面都是不能比较的。 下面是个人在使用时候,对它在数组方面一些操作进行的总结。
1.数组定义
|
1
2
|
[chengmo@centos5 ~]$ a=(1 2 3 4 5)[chengmo@centos5 ~]$ echo $a |
一对括号表示是数组,数组元素用“空格”符号分割开。
2.数组读取与赋值
- 得到长度:
|
1
2
|
[chengmo@centos5 ~]$ echo ${#a[@]}5 |
用${#数组名[@或*]} 可以得到数组长度
- 读取:
|
1
2
|
[chengmo@centos5 ~]$ echo ${a[2]} 3 |
|
1
2
|
[chengmo@centos5 ~]$ echo ${a[*]} 1 2 3 4 5 |
用${数组名[下标]} 下标是从0开始 下标是:*或者@ 得到整个数组内容
- 赋值:
|
1
2
3
4
5
6
7
|
[chengmo@centos5 ~]$ a[1]=100[chengmo@centos5 ~]$ echo ${a[*]} 1 100 3 4 5[chengmo@centos5 ~]$ a[5]=100 [chengmo@centos5 ~]$ echo ${a[*]}1 100 3 4 5 100 |
直接通过 数组名[下标] 就可以对其进行引用赋值,如果下标不存在,自动添加新一个数组元素
- 删除:
|
1
2
3
4
5
6
7
8
9
|
[chengmo@centos5 ~]$ a=(1 2 3 4 5)[chengmo@centos5 ~]$ unset a[chengmo@centos5 ~]$ echo ${a[*]}[chengmo@centos5 ~]$ a=(1 2 3 4 5)[chengmo@centos5 ~]$ unset a[1] [chengmo@centos5 ~]$ echo ${a[*]} 1 3 4 5[chengmo@centos5 ~]$ echo ${#a[*]}4 |
直接通过:unset 数组[下标] 可以清除相应的元素,不带下标,清除整个数据。
3.特殊使用
- 分片:
|
1
2
3
4
5
|
[chengmo@centos5 ~]$ a=(1 2 3 4 5)[chengmo@centos5 ~]$ echo ${a[@]:0:3}1 2 3[chengmo@centos5 ~]$ echo ${a[@]:1:4}2 3 4 5 |
|
1
2
3
4
5
|
[chengmo@centos5 ~]$ c=(${a[@]:1:4})[chengmo@centos5 ~]$ echo ${#c[@]}4[chengmo@centos5 ~]$ echo ${c[*]} 2 3 4 5 |
直接通过 ${数组名[@或*]:起始位置:长度} 切片原先数组,返回是字符串,中间用“空格”分开,因此如果加上”()”,将得到切片数组,上面例子:c 就是一个新数据。
- 替换:
|
1
2
3
4
5
6
7
8
|
[chengmo@centos5 ~]$ a=(1 2 3 4 5) [chengmo@centos5 ~]$ echo ${a[@]/3/100}1 2 100 4 5[chengmo@centos5 ~]$ echo ${a[@]}1 2 3 4 5[chengmo@centos5 ~]$ a=(${a[@]/3/100}) [chengmo@centos5 ~]$ echo ${a[@]} 1 2 100 4 5 |
调用方法是:${数组名[@或*]/查找字符/替换字符} 该操作不会改变原先数组内容,如果需要修改,可以看上面例子,重新定义数据。
从上面讲到的,大家可以发现linux shell 的数组已经很强大了,常见的操作已经绰绰有余了
linux shell在编程方面比windows 批处理强大太多,无论是在循环、运算。已经数据类型方面都是不能比较的。 下面是个人在使用时候,对它在数组方面一些操作进行的总结。
1.数组定义
|
1
2
|
[chengmo@centos5 ~]$ a=(1 2 3 4 5)[chengmo@centos5 ~]$ echo $a |
一对括号表示是数组,数组元素用“空格”符号分割开。
2.数组读取与赋值
- 得到长度:
|
1
2
|
[chengmo@centos5 ~]$ echo ${#a[@]}5 |
用${#数组名[@或*]} 可以得到数组长度
- 读取:
|
1
2
|
[chengmo@centos5 ~]$ echo ${a[2]} 3 |
|
1
2
|
[chengmo@centos5 ~]$ echo ${a[*]} 1 2 3 4 5 |
用${数组名[下标]} 下标是从0开始 下标是:*或者@ 得到整个数组内容
- 赋值:
|
1
2
3
4
5
6
7
|
[chengmo@centos5 ~]$ a[1]=100[chengmo@centos5 ~]$ echo ${a[*]} 1 100 3 4 5[chengmo@centos5 ~]$ a[5]=100 [chengmo@centos5 ~]$ echo ${a[*]}1 100 3 4 5 100 |
直接通过 数组名[下标] 就可以对其进行引用赋值,如果下标不存在,自动添加新一个数组元素
- 删除:
|
1
2
3
4
5
6
7
8
9
|
[chengmo@centos5 ~]$ a=(1 2 3 4 5)[chengmo@centos5 ~]$ unset a[chengmo@centos5 ~]$ echo ${a[*]}[chengmo@centos5 ~]$ a=(1 2 3 4 5)[chengmo@centos5 ~]$ unset a[1] [chengmo@centos5 ~]$ echo ${a[*]} 1 3 4 5[chengmo@centos5 ~]$ echo ${#a[*]}4 |
直接通过:unset 数组[下标] 可以清除相应的元素,不带下标,清除整个数据。
3.特殊使用
- 分片:
|
1
2
3
4
5
|
[chengmo@centos5 ~]$ a=(1 2 3 4 5)[chengmo@centos5 ~]$ echo ${a[@]:0:3}1 2 3[chengmo@centos5 ~]$ echo ${a[@]:1:4}2 3 4 5 |
|
1
2
3
4
5
|
[chengmo@centos5 ~]$ c=(${a[@]:1:4})[chengmo@centos5 ~]$ echo ${#c[@]}4[chengmo@centos5 ~]$ echo ${c[*]} 2 3 4 5 |
直接通过 ${数组名[@或*]:起始位置:长度} 切片原先数组,返回是字符串,中间用“空格”分开,因此如果加上”()”,将得到切片数组,上面例子:c 就是一个新数据。
- 替换:
|
1
2
3
4
5
6
7
8
|
[chengmo@centos5 ~]$ a=(1 2 3 4 5) [chengmo@centos5 ~]$ echo ${a[@]/3/100}1 2 100 4 5[chengmo@centos5 ~]$ echo ${a[@]}1 2 3 4 5[chengmo@centos5 ~]$ a=(${a[@]/3/100}) [chengmo@centos5 ~]$ echo ${a[@]} 1 2 100 4 5 |
调用方法是:${数组名[@或*]/查找字符/替换字符} 该操作不会改变原先数组内容,如果需要修改,可以看上面例子,重新定义数据。
从上面讲到的,大家可以发现linux shell 的数组已经很强大了,常见的操作已经绰绰有余了
转载-Linux Shell 数组建立及使用技巧的更多相关文章
- 转:linux shell 数组建立及使用技巧
linux shell在编程方面比windows 批处理强大太多,无论是在循环.运算.已经数据类型方面都是不能比较的. 下面是个人在使用时候,对它在数组方面一些操作进行的总结. 1.数组定义 [che ...
- linux shell 数组建立及使用技巧
参考网址:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html linux shell在编程方面比windows 批处理强大太多 ...
- Linux Shell数组常用操作详解
Linux Shell数组常用操作详解 1数组定义: declare -a 数组名 数组名=(元素1 元素2 元素3 ) declare -a array array=( ) 数组用小括号括起,数组元 ...
- linux shell 数组的使用
引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...
- Linux Shell 数组
shell 数组一般都是一维数组. 1. 数组的声明 declare -a arr 该命令将声明一个数组arr,实际上不声明也可以直接定义数组. 2. 数组的初始化 arr=(1 2 3):该命令定义 ...
- [转载]Linux shell中的竖线(|)——管道符号
原文地址:Linux shell中的竖线(|)--管道符号作者:潇潇 管道符号,是unix一个很强大的功能,符号为一条竖线:"|". 用法: command 1 | command ...
- linux shell数组
from: http://www.jb51.net/article/34322.htm bash shell只支持一维数组,但参数个数没有限制. 声明一个数组:declare -a array(其实不 ...
- linux shell数组赋值方法(常用)
http://blog.csdn.net/shaobingj126/article/details/7395161 Bash中,数组变量的赋值有两种方法: (1) name = (value1 ... ...
- Linux shell —— 数组与关联数组
使用 declare -A(declare 的用法请使用 help 进行查看,help declare) 进行声明关联数组变量: $ declare -A fruits_price $ fruits_ ...
随机推荐
- 认证客户端的链接与socketserver实现并发
from socket import * import hmac,os secret_key=b'linhaifeng bang bang bang' def conn_auth(conn): ''' ...
- Latex 学习之旅(一)
学习资料: LaTeX笔记(八)--数学建模专题 如何用Markdown写论文? LaTeX排版札记 LaTeX排版札记:part 2-速查手册.导言区.扉页和公式 论文格式细节整理汇总 https: ...
- iOS学习——UIAlertController详解
在开发中,弹出提示框是必不可少的.这两天项目中统一对已经被iOS API废弃的UIAlertView和UIActionSheet进行替换,我们知道,UIAlertView和UIActionSheet都 ...
- Coursera课程 Programming Languages, Part C 总结
碎言碎语 和前面的 ML 和 Racket 感觉明显不一样了,一边学着一边觉得这真是一门奇怪的语言,有着各种奇怪的语法,不过真的算是一个奇妙的体验(相比前面的两门语言,Ruby 的学习资源多了不少). ...
- 【转】Java中super和this的几种用法与区别
1. 子类的构造函数如果要引用super的话,必须把super放在函数的首位. class Base { Base() { System.out.println("Base&qu ...
- Ajax方式分页加载列表实现
在前面: 最近需要用到这个功能,所以这几天一直在研究这个,目前大致功能已实现,后续需要完善,但需要的功能点已完成,记录下: 1.分页功能引入bootstrap的分页插件: <script typ ...
- 把玩爬虫框架Gecco
如果你现在接到一个任务,获取某某行业下的分类. 作为一个非该领域专家,没有深厚的运营经验功底,要提供一套摆的上台面且让人信服的行业分类,恐怕不那么简单. 找不到专家没有关系,我们可以爬虫.把那些专家的 ...
- Wannafly模拟赛 A.矩阵(二分答案+hash)
矩阵 时间限制:1秒 空间限制:131072K 题目描述 给出一个n * m的矩阵.让你从中发现一个最大的正方形.使得这样子的正方形在矩阵中出现了至少两次.输出最大正方形的边长. 输入描述: 第一行两 ...
- Codechef:Path Triples On Tree
Path Triples On Tree 题意是求树上都不相交或者都相交的路径三元组数量. 发现blog里没什么树形dp题,也没有cc题,所以来丢一道cc上的树形dp题. 比较暴力,比较恶心 #inc ...
- Spring框架学习笔记(5)——自动装配
1.通过bean标签的autowire属性可以实现bean属性的自动装配. 创建一个新的Spring配置文件beans-autowire.xml,这里我们配置了3个bean,Address.Car.P ...