成都网站建设设计

将想法与焦点和您一起共享

20.1620.17shell中的函数(上下);20.18-创新互联

20.16 shell中的函数(上)

创新互联公司主营水磨沟网站建设的网络公司,主营网站建设方案,重庆APP开发公司,水磨沟h5微信小程序搭建,水磨沟网站营销推广欢迎水磨沟等地区企业咨询

函数就是把一段代码整理到了一个小单元中,并给这个小单元起

一个名字,当用到这段代码时直接调用这个小单元的名字即可。

1.

[root@hao-01 ~]# vi fun1.sh

添加内容:

#!/bin/bash

function inp(){

echo "The first par is $1"

echo "The second par is $2"

echo "The third par is $3"

echo "the scritp name is $0"

echo "the number of par is $#"

}

inp $1 $2 $3

2. 执行fun1.sh脚本,后面跟函数:

[root@hao-01 ~]# sh fun1.sh 1

20.17 shell中的函数(下)

1. 加法函数:

[root@hao-01 ~]# vi fun2.sh

添加内容:

#!/bin/bash

sum() {

s=$[$1+$2]

echo $s

}

sum 1 10

2. 执行fun2.sh脚本:

[root@hao-01 ~]# sh -x fun2.sh

1. 输入网卡名字,显示网卡ip:

[root@hao-01 ~]# vi fun3.sh

添加内容:

#!/bin/bash

ip()

{

ifconfig |grep -A1 "$1: "|awk '/inet/ {print $2}'

}

read -p "please input the eth name: " ech

ip $eth

2. 执行fun3.sh脚本:

[root@hao-01 ~]# sh fun3.sh

please input the eth name: ens33

20.16 20.17shell中的函数(上下);20.18

20.16 20.17shell中的函数(上下);20.18

20.18 shell中的数组

1. 定义数组:

[root@hao-01 ~]# a=(1 2 3 4 5)

2. 查看a数组的元素:

[root@hao-01 ~]# echo ${a[*]}

20.16 20.17shell中的函数(上下);20.18

20.16 20.17shell中的函数(上下);20.183. 查看数组某个元素的值(数组从0开始值为1):

[root@hao-01 ~]# echo ${a[1]}

20.16 20.17shell中的函数(上下);20.18

4. 获取数组的元素 个数:

[root@hao-01 ~]# echo ${#a[*]}

20.16 20.17shell中的函数(上下);20.18

5. 如果下标不存在则会自动添加一个元素:

[root@hao-01 ~]# a[5]=b

[root@hao-01 ~]# echo ${a[*]}

20.16 20.17shell中的函数(上下);20.18

数组元素赋值(更改替换):

[root@hao-01 ~]# a[5]=bbb

[root@hao-01 ~]# echo ${a[*]}

20.16 20.17shell中的函数(上下);20.18

6. 删除数组元素:

20.16 20.17shell中的函数(上下);20.18

20.16 20.17shell中的函数(上下);20.187. 删除(清空)数组值:

[root@hao-01 ~]# unset a

[root@hao-01 ~]# echo ${a[*]}

20.16 20.17shell中的函数(上下);20.18

8. 设定数组:

[root@hao-01 ~]# a=(`seq 1 10`)

[root@hao-01 ~]# echo ${a[*]}

20.16 20.17shell中的函数(上下);20.18

9. 从第1个元素开始,截取出5个数值:

[root@hao-01 ~]# echo ${a[*]:0:5}

20.16 20.17shell中的函数(上下);20.18

从第2个元素开始,截取出5个数值:

[root@hao-01 ~]# echo ${a[*]:1:5}

20.16 20.17shell中的函数(上下);20.18

10. 从倒数第3个元素开始,截取出2个数值:

[root@hao-01 ~]# echo ${a[*]:0-3:2}

20.16 20.17shell中的函数(上下);20.18

11. 截取替换,8元素打印成cc66:

[root@hao-01 ~]# echo ${a[@]/8/cc66}

20.16 20.17shell中的函数(上下);20.18

12. 替换元素值,8元素替换成cc66:

[root@hao-01 ~]# a=(${a[*]/8/cc66})

[root@hao-01 ~]# echo ${a[*]}

20.16 20.17shell中的函数(上下);20.18

替换元素值,cc66元素替换成888:

[root@hao-01 ~]# a=(${a[*]/cc66/888})

[root@hao-01 ~]# echo ${a[*]}

20.16 20.17shell中的函数(上下);20.18

20.19 告警系统需求分析

1. 需求:使用shell定制各种个性化告警工具,但需要统一化管理、规范化管理。

2. 思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等。

3. 主程序:作为整个脚本的入口,是整个系统的命脉。

4. 配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件。

5. 子程序:这个才是真正的监控脚本,用来监控各个指标。

6. 邮件引擎:是由一个python程序来实现,它可以定义发邮件的服务器、发邮件人以及发件人密码

7. 输出日志:整个监控系统要有日志输出。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:20.1620.17shell中的函数(上下);20.18-创新互联
网页路径:http://chengdu.cdxwcx.cn/article/ppjei.html