转载自: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 数组建立及使用技巧的更多相关文章

  1. 转:linux shell 数组建立及使用技巧

    linux shell在编程方面比windows 批处理强大太多,无论是在循环.运算.已经数据类型方面都是不能比较的. 下面是个人在使用时候,对它在数组方面一些操作进行的总结. 1.数组定义 [che ...

  2. linux shell 数组建立及使用技巧

    参考网址:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html linux shell在编程方面比windows 批处理强大太多 ...

  3. Linux Shell数组常用操作详解

    Linux Shell数组常用操作详解 1数组定义: declare -a 数组名 数组名=(元素1 元素2 元素3 ) declare -a array array=( ) 数组用小括号括起,数组元 ...

  4. linux shell 数组的使用

    引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...

  5. Linux Shell 数组

    shell 数组一般都是一维数组. 1. 数组的声明 declare -a arr 该命令将声明一个数组arr,实际上不声明也可以直接定义数组. 2. 数组的初始化 arr=(1 2 3):该命令定义 ...

  6. [转载]Linux shell中的竖线(|)——管道符号

    原文地址:Linux shell中的竖线(|)--管道符号作者:潇潇 管道符号,是unix一个很强大的功能,符号为一条竖线:"|". 用法: command 1 | command ...

  7. linux shell数组

    from: http://www.jb51.net/article/34322.htm bash shell只支持一维数组,但参数个数没有限制. 声明一个数组:declare -a array(其实不 ...

  8. linux shell数组赋值方法(常用)

    http://blog.csdn.net/shaobingj126/article/details/7395161 Bash中,数组变量的赋值有两种方法: (1) name = (value1 ... ...

  9. Linux shell —— 数组与关联数组

    使用 declare -A(declare 的用法请使用 help 进行查看,help declare) 进行声明关联数组变量: $ declare -A fruits_price $ fruits_ ...

随机推荐

  1. entity framework学习

    资源 Entity Framework技术导游系列开篇与热身

  2. java String 不可变

    关于String不可变的问题也看了很多,最近看了一篇讲的非常好的文章,总结如下 所谓的不可变,并非真的不可变String s = "123"; s = "456" ...

  3. jsp基础了解

    1.什么是动态页面:    所谓的动态网页,是指跟静态网页相对的一种网页编程技术.静态网页,随着html代码的生成,页面的内容和显示效果就基本上不会发生变化了--除非你修改页面代码.而动态网页则不然, ...

  4. 解决打开png图片黑屏问题(批量还原Xcode优化后的png)

    window 打开Xcode 里面的png图片会黑屏,但是在mac 打开就显示正常, 这是因为Xocde里面的png图片被 pngcrush 优化过了,需要还原它的优化,window 平台才可以打开. ...

  5. 机器学习笔记4-Tensorflow线性模型示例及TensorBoard的使用

    前言 在上一篇中,我简单介绍了一下Tensorflow以及在本机及阿里云的PAI平台上跑通第一个示例的步骤.在本篇中我将稍微讲解一下几个基本概念以及Tensorflow的基础语法. 本文代码都是基于A ...

  6. linked lists in .NET

    链表是数据结构中存储数据的一种形式.分为单向链表和双向链表以及循环链表.LinkedList是泛型链表,用节点存取,节点类型为LinkedListNode<T>,每个节点都有Next和Pr ...

  7. 【Docker】安装Docker及基本使用

    该文以CentOS系统为例,介绍Docker安装及基本使用.为了简化安装流程,Docker 官方提供了一套安装脚本,CentOS 系统上可以使用这套脚本安装: curl -sSL https://ge ...

  8. Vijos P1448 校门外的树【多解,线段树,树状数组,括号序列法+暴力优化】

    校门外的树 描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的…… 如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作: K=1,K= ...

  9. sscanf()用法

    http://blog.chinaunix.net/uid-26284412-id-3189214.html #include<cstdio> #include<cstring> ...

  10. three.js 入门案例

    最近公司需要用tree.js实现一个3D图的显示,就看了官方文档,正好有时间,就记录下来. 由于我们公司的前端框架用的是angular,所以我就把我的treejs封装在一个directives里面.后 ...