2018-06-01Linux学习

20.16-17 shell中的函数

函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。

格式: function f_name() {
command
}
函数必须要放在最前面

示例1
#!/bin/bash
input() {
    echo $1 $2 $# $0
}

input 1 a b

示例2
#!/bin/bash
sum() {
    s=$[$1+$2]
    echo $s
}
sum 1 2

示例3
#!/bin/bash
ip() {
    ifconfig |grep -A1 "$1 " |tail -1 |awk '{print $2}'|awk -F':' '{print $2}'
}
read -p "Please input the eth name: " e
myip=ip $e
echo "$e address is $myip"

操作过程

案例1

[root@linux-01 shell]# vim fun1.sh

#!/bin/bash
function inp(){
echo $1 $2 $3 $0 $#
}
inp 1 a 2

[root@linux-01 shell]# sh fun1.sh 
1 a 2 fun1.sh 3

[root@linux-01 shell]# vim 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 scrip name par is $0"
echo "The number of par is $#"
}
inp b a 5 w z x

[root@linux-01 shell]# sh fun1.sh 
The first par is b
The second par is a
The third par is 5
The scrip name par is fun1.sh
The number of par is 6

[root@linux-01 shell]# vim 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 scrip name par is $0"
echo "The number of par is $#"
}
inp $1 $2 $3 $4 $5 $6

[root@linux-01 shell]# sh fun1.sh 6 w
The first par is 6
The second par is w
The third par is 
The scrip name par is fun1.sh
The number of par is 2

[root@linux-01 shell]# sh fun1.sh 6 w d a f e g q
The first par is 6
The second par is w
The third par is d
The scrip name par is fun1.sh
The number of par is 6

案例2

[root@linux-01 shell]# vim fun2.sh

#!/bin/bash
sum(){
s=$[$1+$2]
echo $s
}
sum 1 2

[root@linux-01 shell]# sh fun2.sh 
3

案例3

[root@linux-01 shell]# ifconfig| grep -A1 "ens33: "| tail -1| awk '{print $2}'
192.168.106.160

[root@linux-01 shell]# ifconfig| grep -A1 "ens33: "| tail -1| awk '/inet/ {print $2}'
192.168.106.160

[root@linux-01 shell]# vim fun3.sh

#!/bin/bash
ip() {
ifconfig| grep -A1 "$1: "| tail -1| awk '{print $2}'
}
read -p "Please input the eth name: " eth
myip=ip $eth
echo "$eth address is $myip"

[root@linux-01 shell]# sh fun3.sh 
Please input the eth name: ens33
ens33 address is 192.168.106.160

[root@linux-01 shell]# sh fun3.sh 
Please input the eth name: ens37
ens37 address is 172.16.166.130

20.18 shell中的数组

shell中的数组1

定义数组 a=(1 2 3 4 5); echo ${a[@]}
echo ${#a[@]} 获取数组的元素个数 
echo ${a[2]} 读取第三个元素,数组从0开始
echo ${a[*]} 等同于 ${a[@]}  显示整个数组

数组赋值
a[1]=100; echo ${a[@]}
a[5]=2; echo ${a[@]} 如果下标不存在则会自动添加一个元素
数组的删除
uset a; unset a[1]

shell中的数组2

数组分片
a=(`seq 1 5`)
echo ${a[@]:0:3} 从第一个元素开始,截取3个
echo ${a[@]:1:4} 从第二个元素开始,截取4个
echo ${a[@]:0-3:2} 从倒数第3个元素开始,截取2个
数组替换
echo ${a[@]/3/100}
a=(${a[@]/3/100})

操作过程

定义数组

[root@linux-01 shell]# b=(1 2 3)
[root@linux-01 shell]# echo $b
1
[root@linux-01 shell]# echo ${b[@]}
1 2 3
[root@linux-01 shell]# echo ${b[*]}
1 2 3
[root@linux-01 shell]# echo ${b[0]}
1
[root@linux-01 shell]# echo ${b[1]}
2
[root@linux-01 shell]# echo ${b[2]}
3
[root@linux-01 shell]# echo ${#b[*]}
3

数组赋值

[root@linux-01 shell]# b[3]=a
[root@linux-01 shell]# echo ${b[*]}
1 2 3 a
[root@linux-01 shell]# b[3]=aaa
[root@linux-01 shell]# echo ${b[*]}
1 2 3 aaa

[root@linux-01 shell]# unset b[3]
[root@linux-01 shell]# echo ${b[*]}
1 2 3

[root@linux-01 shell]# unset b
[root@linux-01 shell]# echo ${b[*]}

数组分片

[root@linux-01 shell]# a=(`seq 1 10`)
[root@linux-01 shell]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
[root@linux-01 shell]# echo ${a[@]:3:4}
4 5 6 7
[root@linux-01 shell]# echo ${a[@]:0-3:2}
8 9
[root@linux-01 shell]# echo ${a[@]/8/6}
1 2 3 4 5 6 7 6 9 10

20.19 告警系统需求分析

shell项目-告警系统

需求:使用shell定制各种个性化告警工具,但需要统一化管理、规范化管理。
思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等。
主程序:作为整个脚本的入口,是整个系统的命脉。
配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件。
子程序:这个才是真正的监控脚本,用来监控各个指标。
邮件引擎:是由一个python程序来实现,它可以定义发邮件的服务器、发邮件人以及发件人密码
输出日志:整个监控系统要有日志输出。

要求:我们的机器角色多种多样,但是所有机器上都要部署同样的监控系统,也就说所有机器不管什么角色,整个程序框架都是一致的,不同的地方在于根据不同的角色,定制不同的配置文件。

程序架构:   

(主目录 mon)
1、bin: [main.sh]
2、conf: [ mon.conf]
3、shares: [load.sh 502.sh]
4、mail: [mail.py mail.sh]
5、log: [mon.log  err.log ]

bin下是主程序
conf下是配置文件
shares下是各个监控脚本
mail下是邮件引擎
log下是日志。

猜你喜欢

转载自blog.51cto.com/9298822/2123508