Shell数组

前端之家收集整理的这篇文章主要介绍了Shell数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Shell数组的定义与增删改查

Shell数组就是一个元素集合,它把有限个元素用一个名字来命令,然后用编号对他们进行区分。这个名字就是数组名,用于区分不同内容的编号就称为数组下标。

Shell数组的定义

Shell数组的定义有多种方法
方法1:用小括号将变量值括起来赋值给数组变量,每个变量值之间要用空格进行分割。
语法如下:
array=(value1 value2 value3 ...)
示例:

[root@CentOS7-1511-clone ~]# array=(1 2 3)
[root@CentOS7-1511-clone ~]# echo ${array[*]}
1 2 3

方法2:用小括号将变量值括起来,同时采用键值对的形式赋值
语法如下:
array=([1]=one [2]=two [3]=three)
此种方法为key-value键值对的形式
示例:

[root@CentOS7-1511-clone ~]# array=([1]=one [2]=two [haha]=three)
[root@CentOS7-1511-clone ~]# echo ${array[haha]}
three

这种情况下不能使用数组下标取值

上面的方法还可以使用下面这种形式表示
语法:
array[0]=a;array[1]=b;array[haha]=three
示例:

[root@CentOS7-1511-clone ~]# array[0]=a;array[1]=b;array[haha]=three
[root@CentOS7-1511-clone ~]# echo ${array[haha]}
three

其实跟方法2是一样的

方法3:动态地定义数组变量,并使用命令的输出结果作为数组的内容
语法:
array=($(命令))

array=(命令)
示例:

[root@CentOS7-1511-clone ~]# mkdir -p /array
[root@CentOS7-1511-clone ~]# touch /array/{1..3}.txt
[root@CentOS7-1511-clone ~]# ll /array/
总用量 0
-rw-r--r-- 1 root root 0 4月   9 17:55 1.txt
-rw-r--r-- 1 root root 0 4月   9 17:55 2.txt
-rw-r--r-- 1 root root 0 4月   9 17:55 3.txt
[root@CentOS7-1511-clone ~]# array=($(ls /array))
[root@CentOS7-1511-clone ~]# echo ${array[*]}
1.txt 2.txt 3.txt

Shell数组的定义的使用方法

打印数组元素

[root@CentOS7-1511-clone ~]# array=(one two three)
[root@CentOS7-1511-clone ~]# echo ${array[0]}
one
[root@CentOS7-1511-clone ~]# echo ${array[1]}
two
[root@CentOS7-1511-clone ~]# echo ${array[2]}
three

打印数组元素的个数

[root@CentOS7-1511-clone ~]# echo ${array[*]}  #<==使用*或@可以得到整个数组的内容
one two three
[root@CentOS7-1511-clone ~]# echo ${#array[*]} #<==${#数组名[*]}可以得到数组的长度,这个和变量子串的方法一样,因为数组也是特殊的变量
3

数组赋值

可以通过"数组名[下标]"对数组进行引用赋值,如果下标不存在,则自动添加一个新的数组元素,如果下标存在则覆盖原来的值。
示例:

[root@CentOS7-1511-clone ~]# echo ${array[*]}
one two three
[root@CentOS7-1511-clone ~]# array[3]=four #<==下标3原本不存在
[root@CentOS7-1511-clone ~]# echo ${array[*]}
one two three four

数组的删除

数组的本质还是变量,因此可通过"unset 数组[下标]"清楚相应的数组元素,如果不带下标则表示清楚整个数组的所有数据
示例:

[root@CentOS7-1511-clone ~]# echo ${array[*]}
one two three four
[root@CentOS7-1511-clone ~]# unset array[1]
[root@CentOS7-1511-clone ~]# echo ${array[*]}
one three four

Shell数组脚本开发实践

示例1:

#!/bin/sh
array=(1 2 3 4 5)
for i in ${array[*]}
do
    echo $i
done
结果:
1
2
3
4
5

示例2:找到并删除10天前的日志

#!/bin/bash

#### 加载zdlh函数库和配置项
[ -e $HOME/bin/zd-profile ] && . $HOME/bin/zd-profile

Delete_date=$(date -d "-10 day" +%Y-%m-%d)

#LOGPATH=$(ls -d $HOME/app/zd*/logs/)
LOGPATH_path=($GT_FILE_EUREKA $GT_FILE_CMS $GT_FILE_WEB $GT_FILE_TASK $GT_FILE_MONITOR $GT_FILE_MESSAGE)

for i in ${LOGPATH_path[*]}
do
    [ ! -d ${i}/logs ] && continue
    #检测是否有10天的日志文件
    logfile_count=$(find ${i}/logs -mtime +3 -name "spring.log.*"|wc -l)
    if [ $logfile_count -ne 0 ];then
        #删除创建日期在10天前的日志
        find ${i}/logs -mtime +10 -name "spring.log.*" -exec rm -f {} \;
    else
        #获取spring.log中10天前日志的最后行数
        log_file=${i}/logs/spring.log
        log_line=$(/bin/awk '{t=$1; if(t<"'$Delete_date'") {if(length !=0)print NR}}' ${log_file}|tail -n1)
        if [ -z $log_line ];then
           continue
        else
            #删除spring.log中10天前的内容
            sed -i '1,'$log_line''d ${log_file}
        fi
    fi
done
原文链接:https://www.f2er.com/bash/388264.html

猜你在找的Bash相关文章