首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >快速掌握shell脚本数组,看这篇

快速掌握shell脚本数组,看这篇

作者头像
老油条IT记
发布2020-09-04 17:04:39
5.1K0
发布2020-09-04 17:04:39
举报

#介绍 数组相当于一些元素的集合,可以从中拿取相关的元素数据,将内容放在()小括号里面,数组之间的元素使用空格来分隔,下标从0开始。

#数组的定义

#静态数组
array=(1 2 3 4 5)

#动态数组
array=($(ls))

#为数组赋值
array[3]=4

#数组的打印命令

#打印所有元素
echo ${array[*]} 或 echo ${array[@]}

#打印数组长度
echo ${#array[*]} 或 echo ${#array[@]}

#打印单个元素
echo ${array[i]}

#简单示例 #1.打印数组单个元素的方法:${数组名[下标]}

#例子
[root@game shell]# array=(1 2 3)
[root@game shell]# echo ${array[0]}
1   #数组的下标从0开始
[root@game shell]# echo ${array[1]}
2
[root@game shell]# echo ${array[2]}
3
[root@game shell]# echo ${array[*]}
1 2 3   #使用*可以获取整个数组的内容
[root@game shell]# echo ${array[@]}
1 2 3   #使用@可以获取整个数组的内容

#2、打印数组元素的个数

[root@game shell]# array=(1 2 3)
[root@game shell]# echo ${#array[@]}
3   #定义的数组有3个参数
[root@game shell]# echo ${#array[*]}
3

#3、数组赋值

#通过 "数组名[下标]" 对数组进行引用赋值,
如果下标不存在,则自动添加一个新的数组元素,如果下标存在,则覆盖原来的值
[root@game shell]# array=(1 2 3)
[root@game shell]# echo ${array[@]}
1 2 3
[root@game shell]# echo ${array[*]}
1 2 3
[root@game shell]# array[3]=4   #添加数组
[root@game shell]# echo ${array[*]}
1 2 3 4
[root@game shell]# array[0]=guoke  #修改
[root@game shell]# echo ${array[*]}
guoke 2 3 4

#4.数组的删除

#格式:unset 数组[下标],如果不带下标,将删除整个数组的所有数据
[root@game shell]# echo ${array[*]}
guoke 2 3 4
[root@game shell]# unset array[1]       #取消下标为1的数组元素
[root@game shell]# echo ${array[*]}     #将看到2没有了
guoke 3 4
[root@game shell]# unset array          #删除整个数组
[root@game shell]# echo ${array[*]}

#实践:循环批量输出数组的元素 #使用for循环

[root@game shell]# cat array_2.sh 
#!/bin/bash

array=(1 2 3 4 5)
for i in ${array[*]}
do
    echo $i
done

#执行效果
[root@game shell]# sh array_2.sh 
1
2
3
4
5

#使用C语言的for循环打印

[root@game shell]# cat array_1.sh 
#!/bin/bash

array=(1 2 3 4 5)
for ((i=0;i<${#array[*]};i++))
do
    echo ${array[i]}
done
#说明:首次循环i=0,数组里使用#号统计的个数为5个,然后第一次输出1,接着输出12345,
当i=6的时候,就不小于数组的5个数了,然后就不做输出了

#执行效果
[root@game shell]# sh array_1.sh 
1
2
3
4
5

#使用while循环

[root@game shell]# cat array_3.sh 
#!/bin/bash

array=(1 2 3 4 5)
i=0
while ((i<${#array[*]}))
do
    echo ${array[i]}
    ((i++))
done
#提示:输出和上面的相同

#数组的常见面试题 #利用bash for循环打印下面这句话中字母数不大于5的单词 curl is powered by libcurl for all transfer-related features

#思路

#1.先把所有的单词放在数组里,然后依次进行判断
array=(curl is powered by libcurl for all transfer-related features)

#2.计算变量内容的长度,常见的方法有
[root@game scripts]# echo $char | wc -L
7
[root@game scripts]# echo ${#char}
7
[root@game scripts]# expr length $char
7
[root@game scripts]# echo $char | awk '{print($0)}'
powered
[root@game scripts]# echo $char|awk '{print length($0)}'
7

#通过数组方法实现

#!/bin/bash
array=(curl is powered by libcurl for all transfer-related features)

#1.使用for循环,提示:-L是统计长度,-l是列出行数
for i in ${array[@]}
do
    if [ `echo $i | wc -L` -lt 5 ];then
	echo $i
    fi
done
说明:首先使用for循环遍历出所有单词,然后使用wc -L进行统计,打印单词小于5的

#2.使用expr计算长度
for i in ${array[@]}
do
    if [ `expr length $i` -lt 5 ];then
        echo $i
    fi
done

#3.使用c语言的for循环
for ((i=0;i<${#array[*]};i++))
do
    if [ ${#array[$i]} -lt 5 ];then 
        echo ${array[$i]}
    fi
done

#执行效果
[root@game scripts]# sh array.sh 
curl
is
by
for
all

#使用案例 批量检查多个网站地址是否正常,如果不正常发邮件通知运维人员

#脚本书写
[root@game test]# cat chweb.sh 
#!/bin/bash

DATE=$(date "+%F +%H:%M")
MAIL=guoke@qq.com

array=(
    https://www.baidu.com
    https://www.guoke.comff
    https://www.baudd.comff
)
while ((1==1))
do
    for i in ${array[@]} #使用for循环网站
    do
	wget --timeout=5 --tries=1  $i -q -O /dev/null #进行访问
    	if [ $? -ne 0 ];then #判断返回值,如果不等于0,就是访问失败,发邮件给运维
	    content="$i access fail"
	    echo "date:$DATE" | mail -s "$content" $MAIL
        fi
    done
    exit 1 #检查完退出脚本
done


#配置邮件报警需要安装mailx
[root@game ~]# yum install mailx
[root@game ~]# cat /etc/mail.rc 
set from=guoke@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=guoke@qq.com
set smtp-auth-password=doqimyktjmjphgcc 
#要注意这个密码是在邮件设置那里获得的,而不是你的邮箱密码
set smtp-auth=login

#效果

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 老油条IT记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档