转载-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_ ...
随机推荐
- Python模块学习---Web
import urlparse url = urlparse.urlparse("http://www.python.org/doc/FAQ.html") print url pr ...
- Windows AD域升级方
前面的博客中我谈到了网络的基本概念和网络参考模型,今天我们来谈企业中常用的技术,Windows AD 域,今天我的笔记将重点讲解Windows AD 域的升级和迁移方法,通过3个小实验进行配置,真实环 ...
- 腾讯windows系统服务器
今天用腾讯的服务器搭建起了自己的博客,先看主页效果...简单的ui设计,主要就是要上服务器看看. 说说服务器的搭建: 1.卖,进腾讯云,自己对应的买操作系统的就可以的啦.具体的链接: https ...
- 85、flask之wtforms
本篇导航: wtforms组件的使用 自定义From组件 一.wtforms组件的使用 1.flask中的wtforms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进 ...
- vi/vim 如何添加和删除多行注释
1.进入vi/vim编辑器,按CTRL+V进入可视化模式(VISUAL BLOCK). 2.移动光标上移或者下移,选中多行的开头. 3.选择完毕后,按大写的I键,此时下方会提示进入"inse ...
- Jfinal启动源码解读
本文对Jfinal的启动源码做解释说明. PS:Jfinal启动容器可基于Tomcat/Jetty等web容器启动,本文基于Jetty的启动方式做启动源码的解读和分析,tomcat类似. 入口 JF ...
- 【HTML_标签大全】
HTML标签大全 标签 描述 标签类型 备注 <!--...--> 定义注释 / 单标签 <!DOCTYPE> 定义文档类型 / 单标签 <head></he ...
- Codeforces Round #356 (Div. 1) C. Bear and Square Grid
C. Bear and Square Grid time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 833D Red-black Cobweb【树分治】
D. Red-black Cobweb time limit per test:6 seconds memory limit per test:256 megabytes input:standard ...
- [51nod1329]路径游戏
Snuke与Sothe两个人在玩一个游戏.游戏在一个2*N的网格中进行(2行N列),这个网格中的2N个格子不是黑色就是白色.定义,一条有效路径是指一个完全由白色格子构成的序列,这个序列的第一个网格元素 ...