转载-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_ ...
随机推荐
- Hadoop 新生报道(四) WordCount
WordCount是hadoop里hello word级的第一个程序,作为一个萌新,我也来跑一跑这个,附带针对新人的说明. 所谓WordCount,就是统计一个或几个文档中相同的单 ...
- windows 异常处理
为了程序的健壮性,windows 中提供了异常处理机制,称为结构化异常,异常一般分为硬件异常和软件异常,硬件异常一般是指在执行机器指令时发生的异常,比如试图向一个拥有只读保护的页面写入内容,或者是硬件 ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- IdentityServer(11)- 使用Hybrid Flow并添加API访问控制
关于Hybrid Flow 和 implicit flow 我在前一篇文章使用OpenID Connect添加用户认证中提到了implicit flow,那么它们是什么呢,它和Hybrid Flow有 ...
- Coursera课程 Programming Languages, Part B 总结
Programming Languages, Part A Programming Languages, Part B Part A 笔记 碎言碎语 很多没有写过 Lisp 程序的人都会对 Lisp ...
- 【iOS】控件截图、MP4格式视频流和m3u8格式视频流截取某一帧功能的实现
最近开发遇到一个点击按钮实现直播视频流截屏的功能,去网上查了一下资料,总结了一下iOS中截屏相关的知识,然后自己做了个demo. demo主要实现了3种截屏方法,分别对应三种不同的应用场景. 1.im ...
- 读书笔记《PHP与MySQL程序设计》一
第1章 PHP概述 1.1 历史(PHP4.PHP5.PHP5.3.PHP6[未发布]) 1.2 一般语言特性(实用性.强大功能.可选择性.成本[开源]) 第2章 环境配置 2.1 安装的前提条件( ...
- 终于理解kalman滤波
2017拜拜啦,怎么过元旦呢?当然是果断呆实验室过... 应该是大二的时候首次听说kalman,一直到今天早上,我一看到其5条"黄金公式",就会找各种理由放弃,看不懂呀...但是研 ...
- cs231n spring 2017 lecture9 CNN Architectures 听课笔记
参考<deeplearning.ai 卷积神经网络 Week 2 听课笔记>. 1. AlexNet(Krizhevsky et al. 2012),8层网络. 学会计算每一层的输出的sh ...
- Codeforces Round #404 (Div. 2)(A.水,暴力,B,排序,贪心)
A. Anton and Polyhedrons time limit per test:2 seconds memory limit per test:256 megabytes input:sta ...