本文共 2020 字,大约阅读时间需要 6 分钟。
1、#!/bin/bash
function inp(){ echo "The first parameter is $1"echo "The second parameter is $2"echo "The third parameter is $3"echo "The number of parameter is $#"echo "The script's name is $0"}#定义一个函数inp a b c asd sdg运行结果:
The first parameter is aThe second parameter is bThe third parameter is cThe number of parameter is 5The script's name is f1.sh2、#!/bin/bash
function inp(){ echo "The first parameter is $1"echo "The second parameter is $2"echo "The third parameter is $3"echo "The number of parameter is $#"echo "The script's name is $0"}inp $1 $2 $3运行结果:
[root@centos7 shell]# sh f1.sh a b c dThe first parameter is aThe second parameter is bThe third parameter is cThe number of parameter is 3The script's name is f1.sh3、sum() {
s=$[$1+$2]echo "$s"}sum $1 $2运行结果:
[root@centos7 shell]# sh f1.sh 2 46、#!/bin/sh
ip(){ ifconfig |grep -A1 "$1: " |tail -1|awk '{print $2}'}
read -p 'please network name: ' n
myip=ip $n
echo "input ip:"$myip 运行结果:
[root@centos7 shell]# sh ip.sh please network name: ens37input ip:192.168.136.128所谓数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,在Shell中,用括号来表示数组,数组元素用“空格”符号分割开。
1、定义数组
a=(1,2,3,4,a)2、查看[root@centos7 shell]# echo ${a[*]}1,2,3,4,a数组的分片
[root@centos7 shell]# a=(seq 1 10
) [root@centos7 shell]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 103、从第一个元素开始截取第三个
1 2 3
4、倒数第三个元素开始截取3个
8 9 10
5、更改元素
[root@centos7 shell]# a[1]=90You have new mail in /var/spool/mail/root[root@centos7 shell]# echo ${a[*]}1 90 3 4 5 6 7 8 9 106、新增元素
[root@centos7 shell]# a[11]=11[root@centos7 shell]# echo ${a[*]}1 90 3 4 5 6 7 8 9 10 11需求:使用shell定制各种个性化告警工具,但是需要统一化管理、规范化管理。
思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等等。主程序:作为整个程序的入口;配置文件:是一个控制中心,它用来开关各个子程序,指定各个相关联的日志文件;子程序:这才是真正的监控脚本,用来监控各个指标;邮件引擎:是由一个Python程序来实现,它可以定义发邮件的服务器、发邮件人以及发邮件密码;输出日志:整个监控系统要有日志输出。要求:
机器的角色多种多样,但是所有的机器上要部署同样的监控系统,也就是说所有的机器不管什么角色,整个程序框架是一样的,不同的地方在于根据不同的角色定制不同的配置文件。程序架构:
bin:主程序
conf:配置文件shares:各个监控脚本mail:邮件引擎log:日志转载于:https://blog.51cto.com/jacksoner/2045919