十六周五次课

Shell脚本基础

20.16/20.17 shell中的函数

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

function f_name() {  
command  
}  

函数必须要放在最前面

linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。

shell中函数的定义格式如下:

[ function ] funname [()]

{

action;

[return int;]

}

说明:

1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。

2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255)

函数返回值在调用该函数后通过 $? 来获得。

注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。

[root@aiker02 script]# vim fun2.sh

#!/bin/bash

funpar() {

echo $1 $2 $3 $0 $#

}

funpar 1 2 3 a b
[root@aiker02 script]# sh !$

sh fun2.sh

1 2 3 fun2.sh 5

在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数…

[root@aiker02 script]# vim fun1.sh

#!/bin/bash

funWithParam(){

echo "第一个参数为 $1 !"

echo "第二个参数为 $2 !"

echo "第十个参数为 $10 !"

echo "第十个参数为 ${10} !"

echo "第十一个参数为 ${11} !"

echo "参数总数有 $# 个!"

echo "作为一个字符串输出所有参数 $* !"

}

funWithParam 1 2 3 4 5 6 7 8 9 34 73
[root@aiker02 script]# sh fun1.sh

第一个参数为 1 !

第二个参数为 2 !

第十个参数为 10 !

第十个参数为 34 !

第十一个参数为 73 !

参数总数有 11 个!

作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !

注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

参数处理

说明

$#

传递到脚本的参数个数

$*

以一个单字符串显示所有向脚本传递的参数

$$

脚本运行的当前进程ID号

$!

后台运行的最后一个进程的ID号

$@

与$*相同,但是使用时加引号,并在引号中返回每个参数。

$-

显示Shell使用的当前选项,与set命令功能相同。

$?

显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。

[root@aiker02 script]# vim fun3.sh

#!/bin/bash

funWithParam(){

echo "第一个参数为 $1 !"

echo "第二个参数为 $2 !"

echo "第十个参数为 $10 !"

echo "第十个参数为 ${10} !"

echo "第十一个参数为 ${11} !"

echo "参数总数有 $# 个!"

echo "作为一个字符串输出所有参数 $* !"

}

funWithParam $1 $3 $3 $4
[root@aiker02 script]# sh fun3.sh 1

第一个参数为 1 !

第二个参数为  !

第十个参数为 10 !

第十个参数为  !

第十一个参数为  !

参数总数有 1 个!

作为一个字符串输出所有参数 1 !

(下)

shell函数功能。函数通常也称之为子过程(subroutine), 其定义格式如下:

funcname() 
{ 
    command 
    ... 
    command; #分号 
} 

定义函数之后,可以在shell中对此函数进行调用,使用函数定义可以将一个复杂的程序分 为多个可管理的程序段。在对函数命名时最好能使用有含义的名字,即函数名能够比较准确的描述函数所完成 的任务。 . 为了程序的维护方便,请尽可能使用注释
例:

iscontinue() 
    { 
        while true 
        do 
            echo -n "Continue?(Y/N)" 
            read ANSWER 
            case $ANSWER in 
            [Yy]) return 0;; 
            [Nn]) return 1;; 
            *) echo "Answer Y or N";; 
            esac 
        done 
} 

这样可以在shell编程中调用iscontinue确定是否继续执行:

if iscontinue 
then continue 
else break 
fi 
[root@aiker02 script]# cat !$
cat fun6.sh
#!/bin/bash
willcontinue() {
    while :
    do
        echo -n "Continue? (y/n)"
        read ANSWER
        case $ANSWER in
        [Yy]) return 0
        ;;
        [Nn]) return 1
        ;;
        *) echo "Answer Y or N";;
        esac
     done
} 
if willcontinue
then
    continue
else
    break
fi
[root@aiker02 script]# sh fun6.sh 
Continue? (y/n)
Answer Y or N
Continue? (y/n)y
[root@aiker02 script]# sh fun6.sh 
Continue? (y/n)n
[root@aiker02 script]# 

shell函数与shell程序非常相似,但二者有一个非常重要的差别:shell程序是由子shell 执行的,而shell函数则是作为当前shell的一部分被执行的,因此在当前shell中可以改 变函数的定义。此外在任意shell(包括交互式的shell)中均可定义函数 。例:

dir () { 
> echo "Permission Link Owner Group File_SZ LastAccess FileName" 
> echo "-----------------------------------------------------------" 
> ls -l $*; 
> } 
dir 

通常情况下,shell script是在子shell中执行的,困此在此子shell中对变量所作的 修改对父shell不起作用。点(.) 命令使用shell在不创建子shell而由当前shell读取 并执行一个shell script, 可以通过这种方式来定义函数及变量。此外点(.)命令最 常用的功能就是通过读取.profile来重新配置初始化login变量。

[root@aiker02 script]# vim fun4.sh
#!/bin/bash
sum() {
      s=$[ $1 + $2 ]
}
sum 1 10
echo $s

[root@aiker02 script]# sh !$
sh fun4.sh
11

提取IP函数

[root@aiker02 script]# !vim
vim fun5.sh
#!/bin/bash
myip() {
        ifconfig | grep -A1 "$1: " | awk '/inet/ {print $2}'
}
read -p "please input the eth name:" e
myip_=`myip $e`
echo "$e ip address is $myip_"

[root@aiker02 script]# !sh
sh fun5.sh
please input the eth name:eth0
eth0 ip address is 172.18.111.154
[root@aiker02 script]# sh fun5.sh  
please input the eth name:lo
lo ip address is 127.0.0.1
[root@aiker02 script]# sh fun5.sh    
please input the eth name:lo0
lo0 ip address is 

加入判断
判断网卡是否存在,是否有IP

[root@test220 script]# !vim
vim ifwhile.sh
#!/bin/bash
myip() {
        ifconfig | grep -A1 "$1: " | awk '/inet/ {print $2}'
}
while :
do
    read -p "please input the eth name:" e
    n=`ifconfig | grep "$e: "`
    if [ -z $e ]
    then
        echo You must input your eth name.
    elif [ -z "$n" ]; then
        echo $e is not exist,you need input the right eth name.
        continue
    else
        break
    fi
done
echo your eth is $e
myip_=`myip $e`
if [ -z "$myip_" ]
then
    echo "$e doesn't have IP."
else
    echo "$e ip address $myip_"
fi

测试:

[root@test220 script]# sh ifwhile.sh 
please input the eth name:
You must input your eth name.
please input the eth name:11
11 is not exist,you need input the right eth name.
please input the eth name:eth1
your eth is eth1
eth1 doesn't have IP.
[root@test220 script]# sh ifwhile.sh 
please input the eth name:eth0
your eth is eth0
eth0 ip address 172.16.22.220

20.18 shell中的数组

数组中可以存放多个值。Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似)。
与大部分编程语言类似,数组元素的下标由0开始。
Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下:

定义数组 a=(1 2 3 4 5); echo ${a[@]}

a=(1 2 3 4 5); echo ${a[@]}
1 2 3 4 5

echo ${#a[@]} # 获取数组的元素个数

[root@aiker02 ~]# echo ${#a[@]}
5

echo ${a[2]} #读取第三个元素,数组从0开始

[root@aiker02 ~]# echo ${a[2]}
3

echo ${a[*]} 等同于 ${a[@]} 显示整个数组

[root@aiker02 ~]# echo ${a[*]}
1 2 3 4 5

数组赋值
a[1]=100; echo ${a[@]}

[root@aiker02 ~]# a[1]=111;echo ${a[1]}  
111
[root@aiker02 ~]# echo ${a[@]}
1 111 3 4 5

a[5]=2; echo ${a[@]} #如果下标不存在则会自动添加一个元素

[root@aiker02 ~]# a[5]=666;echo ${a[@]}
1 111 3 4 5 666

数组的删除
uset a; unset a[1]

[root@aiker02 ~]# unset a[0] ##删除元素
[root@aiker02 ~]# echo ${a[@]}
111 3 4 5 666
[root@aiker02 ~]# unset a  ##删除数组
[root@aiker02 ~]# echo ${a[@]} ##已经不存在了

数组分片
a=(seq 1 5)

[root@aiker02 ~]# a=(`seq 1 5`)
[root@aiker02 ~]# echo ${a[@]}
1 2 3 4 5

echo ${a[@]:0:3} #从第一个元素开始,截取3个

[root@aiker02 ~]# echo ${a[@]:0:3} 
1 2 3

echo ${a[@]:1:4} #从第二个元素开始,截取4个

[root@aiker02 ~]# echo ${a[@]:1:4}
2 3 4 5

echo ${a[@]:0-3:2} #从倒数第3个元素开始,截取2个

[root@aiker02 ~]# echo ${a[@]:0-3:2}
3 4

数组替换
echo ${a[@]/3/100}

[root@aiker02 ~]# echo ${a[@]/3/100}
1 2 100 4 5
[root@aiker02 ~]# echo ${a[@]}
1 2 3 4 5

a=(${a[@]/3/100}) ##3是数组的值,100是被替换后的值

[root@aiker02 ~]# a=(${a[@]/3/100})
[root@aiker02 ~]# echo ${a[@]}     
1 111 100 4 5

20.19 告警系统需求分析

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

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

                                  (主目录 mon)
                ____________________|_______________________________
                |          |             |                          |                                           |
               bin    conf    shares                  mail                                       log
                |          |             |                           |                                            |
[main.sh] [ mon.conf] [load.sh 502.sh]  [mail.py mail.sh] [  mon.log  err.log ]

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

转载于:https://blog.51cto.com/235571/2140100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值