转载自: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. linux 常见操作指令

    1.ssh root@ip ssh 登录 2.ll ls 列出当文件夹下 所以文件 3. cd ./xx 进入 xx 文件夹 4. vim vi xxx 进入 xx文件的 编辑模式. i 开始编辑 e ...

  2. msgpack库的神奇用法

    一般来说,我们会把头部和实际消息分开定义,因为内部工作的worker之间发送消息有些额外的字段,这些字段不属于实际的消息.这时候我们会把worker消息中一个字段定义为interface{}或者obj ...

  3. CSS(一) 引入方式 选择器 权重

    Css(一) Cascading Style Sheet 层叠样式表 css注释方式/*  */ 一.Css引入方式 1. 行间样式 style=" key:value; " &l ...

  4. js中的伪数组

    一, 伪数组 1. 具有length属性 2. 按索引方式存储数据 3. 不具有数组的方法, 比如push(),pop()等 二, 生成伪数组的方法 在js中生成伪数组的方法比较多 1. functi ...

  5. String常用的方法

    l String: 字符串类,字符串是常量:它们的值在创建之后不能更改 l 方法 boolean equals(Object obj) 判断两个字符串中的内容是否相同 boolean equalsIg ...

  6. JAVA基础-IO流(二)

    一.字节流 字节流是通过字节来进行读写操作的,他的使用对象相比于字符流来说更加的广泛.这主要是因为他们读写文件的方式而决定的.字符流读写文件时是将读取到的字节通过默认编码表转换成字符,在通过默认编码表 ...

  7. Java与算法之(1) - 冒泡排序

    冒泡排序法的原理是,每次比较相邻的两个元素,如果它们的顺序错误就把它们交换过来. 例如对4 3 6 2 7 1 5这7个数字进行从小到大的排序,从最左侧开始,首先比较4和3 因为是从小到大排序,4和3 ...

  8. Generator函数语法解析

    转载请注明出处: Generator函数语法解析 Generator函数是ES6提供的一种异步编程解决方案,语法与传统函数完全不同.以下会介绍一下Generator函数. 写下这篇文章的目的其实很简单 ...

  9. dfs学习总结

    今天做到了dfs的训练,感觉和bfs有相似之处,接下来用一道题来总结一下方法,可类比bfs. 上题: Description There is a rectangular room, covered ...

  10. HDU_5563Clarke and five-pointed star

    Clarke and five-pointed star Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...