2016-08-22
授课内容:
数组
高级字符串操作
一、数组
变量:存储单个元素的内存空间
数组:存储多个元素的连续的内存空间,相当于多个变量的集合。
数组名和索引
索引:编号从0开始,属于数值索引
bash的数组支持稀疏格式(索引不连续)
1、声明数组:
declare -a ARRAY_NAME(并非强制声明,但最好按规范声明)
declare -A ARRAY_NAME: 关联数组
2、数组元素的赋值:
(1) 一次只赋值一个元素;
ARRAY_NAME[INDEX]=VALUE
[19:41 root@Centos7.2~]# arr=(1 2 3 4 5)
[19:41 root@Centos7.2~]# echo ${arr[@]}
1 2 3 4 5
[19:41 root@Centos7.2~]# arr[5]=88
[19:41 root@Centos7.2~]# echo ${arr[@]}
1 2 3 4 5 88
[19:41 root@Centos7.2~]# arr[10]=188
[19:42 root@Centos7.2~]# echo ${arr[@]}
1 2 3 4 5 88 188(2) 一次赋值全部元素:
ARRAY_NAME=("VAL1" "VAL2" "VAL3" ...)
[19:41 root@Centos7.2~]# arr=(1 2 3 4 5)
[19:41 root@Centos7.2~]# echo ${arr[@]}
1 2 3 4 53、引用数组:
引用数组元素:${ARRAY_NAME[INDEX]},注意:省略[INDEX]表示引用下标为0的元素
[19:43 root@Centos7.2~]# echo ${arr[@]}
1 2 3 4 5 88 188
[19:43 root@Centos7.2~]# echo ${arr[1]}
2数组的长度(数组中元素的个数):
${#ARRAY_NAME[*]}
${#ARRAY_NAME[@]}
[19:44 root@Centos7.2~]#echo ${arr[@]}
1 2 3 4 5 88 188
[19:44 root@Centos7.2~]# echo ${#arr[@]}
7
[19:44 root@Centos7.2~]# echo ${#arr[*]}
74、数组数据处理:切片
数组切片:${ARRAY[@]:offset:number}
offset: 要跳过的元素个数
number: 要取出的元素个数
取偏移量之后的所有元素${ARRAY[@]:offset}
[19:46 root@Centos7.2~]# echo ${arr[@]}
1 2 3 4 5 88 188
[19:46 root@Centos7.2~]# echo ${arr[@]:2:3}
3 4 5 #跳过两个元素,取后面三个元素5、删除数组中的某元素:导致稀疏格式
unset ARRAY[INDEX]
二、字符串处理
1、字符串切片:
${#var}:返回字符串变量var的长度
${var:offset}:返回字符串变量var中从第offset个字符后(不包括第offset个字符)的字符开始,到最后的部分,offset的取值在0 到${#var}-1 之间
${var:offset:number}:返回字符串变量var中从第offset个字符后(不包括第offset个字符)的字符开始,长度为number的部分
${var: -lengh}:取字符串的最右侧几个字符:
注意:冒号后必须有一空白字符
[19:50 root@Centos7.2~]# str="nihaolinux"
[19:51 root@Centos7.2~]# echo $str
nihaolinux
[19:51 root@Centos7.2~]# echo ${#str}
10 #取字符串的长度
[19:51 root@Centos7.2~]# echo ${str:2}
haolinux #从第二个字符串(不包括第二个)开始到最后
[19:51 root@Centos7.2~]# echo ${str:2:3}
hao #从第二个字符串(不包括第二个)开始取后三个字符
[19:51 root@Centos7.2~]# echo ${str: -5}
linux#取最后侧起5个字符2、基于模式取子串:
(1)${var#*word}:其中word可以是指定的任意字符
功能:自左而右,查找var变量所存储的字符串中,第一次出现的word, 删除字符串开头至第一次出现word字符之间的所有字符
(2)${var##*word}
同上,不同的是,删除的是字符串开头至最后一次由word指定的字符之间的所有内容
(3)${var%word*}:其中word可以是指定的任意字符;
功能:自右而左,查找var变量所存储的字符串中,第一次出现的word, 删除字符串最后一个字符向左至第一次出现word字符之间的所有字符;
(4)${var%%word*}
同上,只不过删除字符串最右侧的字符向左至最后一次出现word字符之间的所有字符;
[19:55 root@Centos7.2~]# str=`getent passwd root`
[19:56 root@Centos7.2~]# echo $str
root:x:0:0:,,62985600:/root:/bin/bash
[19:56 root@Centos7.2~]# echo ${str#*root} #删除第一次出现root(包含)字符串及之前的字符
:x:0:0:,,62985600:/root:/bin/bash
[19:56 root@Centos7.2~]# echo ${str##*root} #删除最后出现root(包含)字符串及之前的字符
:/bin/bash
[19:56 root@Centos7.2~]# echo ${str%root*} #删除从右到左第一次出现root(包含)字符串及之前的字符
root:x:0:0:,,62985600:/
[19:56 root@Centos7.2~]# echo ${str%%root*} #删除从右到左最后出现root(包含)字符串及之前的字符3、查找替换
(1)${var/pattern/substi}:
查找var所表示的字符串中,第一次被pattern所匹配到的字符串,以substi替换之
(2)${var//pattern/substi}:
查找var所表示的字符串中,所有能被pattern所匹配到的字符串,以substi替换之
(3)${var/#pattern/substi}:
查找var所表示的字符串中,行首被pattern所匹配到的字符串,以substi替换之
(4)${var/%pattern/substi}:
查找var所表示的字符串中,行尾被pattern所匹配到的字符串,以substi替换之
[19:57 root@Centos7.2~]# echo ${str/root/ROOT}
ROOT:x:0:0:,,62985600:/root:/bin/bash
[20:01 root@Centos7.2~]# echo ${str//root/ROOT}
ROOT:x:0:0:,,62985600:/ROOT:/bin/bash
[20:02 root@Centos7.2~]# echo ${str/#root/ROOT}
ROOT:x:0:0:,,62985600:/root:/bin/bash
[20:02 root@Centos7.2~]# echo ${str/%bash/BASH}
root:x:0:0:,,62985600:/root:/bin/BASH4、查找并删除
${var/pattern}:查找var所表示的字符串中,删除第一次被pattern所匹配到的字符串
${var//pattern}:所有
${var/#pattern}:首行
${var/%pattern}:行尾
[20:02 root@Centos7.2~]# echo ${str/root}
:x:0:0:,,62985600:/root:/bin/bash
[20:05 root@Centos7.2~]# echo ${str//root}
:x:0:0:,,62985600:/:/bin/bash
[20:05 root@Centos7.2~]# echo ${str#root}
:x:0:0:,,62985600:/root:/bin/bash
[20:06 root@Centos7.2~]# echo ${str%bash}
root:x:0:0:,,62985600:/root:/bin/三、创建临时文件
mktemp命令:创建的临时文件可避免冲突
mktemp[OPTION]... [TEMPLATE]
TEMPLATE: filename.XXX
【X至少要出现三个】
OPTION:
-d: 创建临时目录
--tmpdir=/DIR:指明临时文件所存放的目录位置
实例:
#mktemp--tmpdir=/testdirtest.XXXXXX
本文出自 “6638225” 博客,转载请与作者联系!
原文:http://6638225.blog.51cto.com/6628225/1841224