转:linux shell 数组建立及使用技巧
linux shell在编程方面比windows 批处理强大太多,无论是在循环、运算。已经数据类型方面都是不能比较的。 下面是个人在使用时候,对它在数组方面一些操作进行的总结。
1.数组定义
[chengmo@centos5 ~]$ a=(1 2 3 4 5)
[chengmo@centos5 ~]$ echo $a
1
一对括号表示是数组,数组元素用“空格”符号分割开。
2.数组读取与赋值
- 得到长度:
[chengmo@centos5 ~]$ echo ${#a[@]}
5
用${#数组名[@或*]} 可以得到数组长度
- 读取:
[chengmo@centos5 ~]$ echo ${a[2]}
3
[chengmo@centos5 ~]$ echo ${a[*]}
1 2 3 4 5
用${数组名[下标]} 下标是从0开始 下标是:*或者@ 得到整个数组内容
- 赋值:
[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
直接通过 数组名[下标] 就可以对其进行引用赋值,如果下标不存在,自动添加新一个数组元素
- 删除:
[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.特殊使用
- 分片:
[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
[chengmo@centos5 ~]$ c=(${a[@]:1:4})
[chengmo@centos5 ~]$ echo ${#c[@]}
4
[chengmo@centos5 ~]$ echo ${c[*]}
2 3 4 5
直接通过 ${数组名[@或*]:起始位置:长度} 切片原先数组,返回是字符串,中间用“空格”分开,因此如果加上”()”,将得到切片数组,上面例子:c 就是一个新数据。
- 替换:
[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 的数组已经很强大了,常见的操作已经绰绰有余了。
转自:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html
后续:
http://www.cnblogs.com/chengmo/archive/2010/09/30/1839668.html
转:linux shell 数组建立及使用技巧的更多相关文章
- 转载-Linux Shell 数组建立及使用技巧
转载自:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html 如侵犯版权,请联系我删除 linux shell在编程方面比win ...
- 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数组
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_ ...
- linux shell实用常用命令
本文主要介绍Linux Shell的一些使用小技巧收集,非常实用,需要的朋友可以参考下. 查看本机某端口是否被占用 netstat -anpt | grep 22 查看远程某端口是否被开放 echo ...
随机推荐
- [LeetCode] 237. Delete Node in a Linked List 解题思路
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- tomcat主目录
简单显示天气预报js 代码
- TypeScript 素描 - 装饰器
/* 装饰器 简单理解为C#中的Attribute 可以装饰到类.函数.讯问符.属性.参数上 语法 @xxx 装饰器其实是一个函数 @xxx 就要有一个 function xxx 多个装饰器可以用来装 ...
- 计算app内部缓存文件大小
#pragma mark - 计算单个文件大小 - (long long)fileSizeAtPath:(NSString*)filePath{ NSFileManager* manager = [N ...
- asp.net 分页类
PaginatedList.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; n ...
- Android应用开发-小巫CSDN博客client之嵌入有米广告
Android应用开发-小巫CSDN博客client之嵌入有米广告 上一篇博客给大家介绍怎样集成友盟社会化组件,本篇继续带来干货,教大家怎样嵌入广告到应用中去.小巫自称专业对接30年,熟悉各大渠道SD ...
- Python序列的方法(转)
在快速教程中,我们了解了最基本的序列(sequence).回忆一下,序列包含有定值表(tuple)和表(list).此外,字符串(string)是一种特殊的定值表.表的元素可以更改,定值表一旦建立,其 ...
- mysql常用操作 mysql备份与恢复
先登录mysql ==>mysql -uroot -p 查看数据库的版本 select version(); 查看有哪些库 show datases; 查看当前处于哪个库 select da ...
- css快捷方式
本来是年前准备整理发布的,都搞定50%了,一篇万恶的<盗墓笔记:九幽将军>让我猪油蒙了心.....诶,不说了,搞一半就算了,最后还忘了保存,此刻只听得那一万只草某马呼啸而过... 言归正传 ...
- PullToRefresh的使用
主界面↓ package com.wangzhen.pulltorefresh; import java.util.ArrayList; import java.util.List; import c ...