shell中的函数、shell的数组、告警系统需求分析

shell中的函数

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

格式: function f_name() {
 ?? ?? ?? ?? ?? ?? ? command? ?? ?? ?? ?
        }
函数必须要放在最前面
  • 脚本示例1(用来定义函数打印参数)
[[email protected] aming]# vim fun1.sh
[[email protected] aming]# cat fun1.sh
#!/bin/bash
function inp(){
    echo $1 $2 $3 $0 $#
}

inp 1 a 2
[[email protected] aming]# sh fun1.sh
1 a 2 fun1.sh 3
[[email protected] aming]# sh -x fun1.sh
+ inp 1 a 2
+ echo 1 a 2 fun1.sh 3
1 a 2 fun1.sh 3

[[email protected] aming]# vi fun1.sh
[[email protected] aming]# cat 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 b a 2 3 adf
[[email protected] aming]# sh fun1.sh
The first par is b
The second par is a
The third par is 2
the scritp name is fun1.sh
the number of par is 5
[[email protected] aming]# sh -x fun1.sh
+ inp b a 2 3 adf
+ echo ‘The first par is b‘
The first par is b
+ echo ‘The second par is a‘
The second par is a
+ echo ‘The third par is 2‘
The third par is 2
+ echo ‘the scritp name is fun1.sh‘
the scritp name is fun1.sh
+ echo ‘the number of par is 5‘
the number of par is 5
[[email protected] aming]# 
  • 脚本示例2(用来定义一个加法的函数)
[[email protected] aming]# vim fun2.sh
[[email protected] aming]# cat fun2.sh
#!/bin/bash
sum() {
    s=$[$1+$2]
    echo $s
}

sum 1 2
[[email protected] aming]# sh fun2.sh
3
[[email protected] aming]# sh -x fun2.sh
+ sum 1 2
+ s=3
+ echo 3
3
[[email protected] aming]# 
  • 脚本示例3(用来定义显示IP)
[[email protected] aming]# vim fun3.sh
[[email protected] aming]# cat fun3.sh
#!/bin/bash
ip()
{
    ifconfig |grep -A1 "$1: "|awk ‘/inet/ {print $2}‘
}

read -p "Please input the eth name: " eth
ip $eth
[[email protected] aming]# sh fun3.sh
Please input the eth name: ens33
172.16.111.100
[[email protected] aming]# sh -x fun3.sh
+ read -p ‘Please input the eth name: ‘ eth
Please input the eth name: ens33
+ ip ens33
+ grep -A1 ‘ens33: ‘
+ awk ‘/inet/ {print $2}‘
+ ifconfig
172.16.111.100

shell中的数组

1.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[@]} 如果下标不存在则会自动添加一个元素
    数组的删除
  • unset a; unset a[1]
[[email protected] aming]# b=(1 2 3)
[[email protected] aming]# echo ${b[@]}
1 2 3
[[email protected] aming]# echo ${b[*]}
1 2 3
[[email protected] aming]# echo ${b[1]}
2
[[email protected] aming]# echo ${b[2]}
3
[[email protected] aming]# echo ${b[0]}
1
[[email protected] aming]# echo ${#b[@]}
3
[[email protected] aming]# b[3]=a
[[email protected] aming]# echo ${b[*]}
1 2 3 a
[[email protected] aming]# b[3]=aaa
[[email protected] aming]# echo ${b[*]}
1 2 3 aaa
[[email protected] aming]# unset b[3]  //删除数组
[[email protected] aming]# echo ${b[*]}
1 2 3
[[email protected] aming]# unset b
[[email protected] aming]# echo ${b[*]}

[[email protected] aming]#
2.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})
[[email protected] aming]# a=(`seq 1 10`)
[[email protected] aming]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
[[email protected] aming]# echo ${a[@]:3:4}
4 5 6 7
[[email protected] aming]# echo ${a[@]:0-3:2}
8 9
[[email protected] aming]# echo ${a[@]/8/6}
1 2 3 4 5 6 7 6 9 10
[[email protected] aming]# a=(${a[@]/8/6})
[[email protected] aming]# echo ${a[@]}
1 2 3 4 5 6 7 6 9 10
[[email protected] aming]# 

告警系统需求分析

1.shell项目-告警系统
  • 需求:使用shell定制各种个性化告警工具,但需要统一化管理、规范化管理。
  • 思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等。
  • 主程序:作为整个脚本的入口,是整个系统的命脉。
  • 配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件。
  • 子程序:这个才是真正的监控脚本,用来监控各个指标。
  • 邮件引擎:是由一个python程序来实现,它可以定义发邮件的服务器、发邮件人以及发件人密码
  • 输出日志:整个监控系统要有日志输出。
  • 要求:我们的机器角色多种多样,但是所有机器上都要部署同样的监控系统,也就说所有机器不管什么角色,整个程序框架都是一致的,不同的地方在于根据不同的角色,定制不同的配置文件。

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

原文地址:http://blog.51cto.com/taoxie/2070043

时间: 2024-08-24 01:40:07

shell中的函数、shell的数组、告警系统需求分析的相关文章

shell中的函数 shell中的数组 告警系统需求分析

一.shell中的函数[[email protected] aming]# cd /root/shell/aming[[email protected] aming]# vim fun1.sh //需要注意函数名不能跟shell中的一些关键字冲突#!/bin/bashfunction inp(){echo $1 $2 $3 $0 $#} inp 1 a 2[[email protected] aming]# sh fun1.sh1 a 2 fun1.sh 3 //$0是脚本名称.3是参数个数 继

shell中的函数、shell中的数组、告警系统需求分析

20.16-20.17 shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function f_name() { command } 函数必须要放在最前面 脚本实例1 #!/bin/bash function inp(){ echo $1 $2 $3 $0 $# } inp 1 a 2 脚本实例2 #!/bin/bash sum() { s=$[$1+$2] echo $s } sum 1 2 脚本实例3

20.16/20.17 shell中的函数20.18 shell中的数组20.19 告警系统需求分析

20.16/20.17 shell中的函数显示变量的隐函数执行的结果参数只有一个1,执行的结果 第一个是1,第二个是空的,第三个也是空的,肢本的名字是fun1.sh,总共有1个参数加法的函数显示IP的隐函数-A1是显示关键词的一行包括下一行精准查找到IP脚本就可以这样写,ip $eth 是输入一个网卡的名字#!/bin/baship() {ifconfig |grep -A1 "ens33: " |awk '/inet/ {print $2}'}read -p "Please

七十、shell中的函数、shell中的数组、告警系统需求分析

一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可.函数就是一个子shell,一个代码段. shell中的函数必须要定义在脚本的最上面. 格式: function f_name() { command }函数必须要放在最前面 f_name:函数名,最好不要和shell中的一些关键词冲突,比如for. command:具体命令 function这个单词可以省略掉的. 示例1 #!//bin/bash functio

shell中的函数、数组及告警系统需求分析

一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function f_name() { command } 注意:函数必须要放在最前面!function可以省略直接写函数名. 1.示列:打印shell的参数,函数input() #!/bin/bash input() { ? ? echo $1 $2 $# $0 } input 1 a b $1 表示第一个参数,$0表示文件名 $#表示参数的个数编辑

shell中的函数、数组、告警系统脚本

1.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这 个小单元的名字即可.格式: function f_name() {commond}   函数必须要放在脚本的最前面案例1: #!/bin/bashinput() {echo "第一个参数是$1"echo "第二个参数是$2"echo "总共有多少个参数 $#"echo "这个脚本的名字是 $0"}read -p

20.16-20.17 shell中的函数;20.18 shell中的数组;20.19 告警系统需求

20.16 shell中的函数(上) 函数就是把一段代码整理到了一个小单元中,并给这个小单元起 一个名字,当用到这段代码时直接调用这个小单元的名字即可. 1. [[email protected] ~]# 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&q

20.16/20.17 shell中的函数 20.18 shell中的数组 20.19 告警系统需求

20.16/20.17 shell中的函数函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可.格式: function f_name() {? ?? ?? ?? ?? ?? ?? ? command? ?? ?? ?? ? }函数必须要放在最前面示例1 #!/bin/bashinput() {? ? echo $1 $2 $# $0} input 1 a b 示例2 #!/bin/bashsum() {? ? s=$[$1+$2]? ?

shell 中的函数优先于 命令

在编译Android的时候 把下面的函数,添加到 envsetup.sh, 再运行 build/envsetup.sh 让其生效 function make() { local make=$(which make) echo "you are running function make" ${make} [email protected] } 而后运行 make, 运行的并不是可执行文件 make, 而是 envsetup 中定义的 make函数 在运行完make之后,如何还想执行别的