十七周三次课python
20.16/20.17shell中的函数linux
20.18shell中的数组shell
20.19告警系统需求分析vim
20.16/20.17shell中的函数数组
把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字便可。bash
函数就是一个子shell,就是一个代码段,定义完函数就能够引用它服务器
格式:架构
格式: function f_name() { command } 函数必需要放在最前面
[root@tianqi-02 shell]# vim fun1.sh框架
#!/bin/bash
input(){
echo $1 $2 $3 $0 $#
}
input 1 a 2函数
[root@tianqi-02 shell]# sh fun1.sh
1 a 2 fun1.sh 3
[root@tianqi-02 shell]#
[root@tianqi-02 shell]# sh -x fun1.sh
+ input 1 a 2
+ echo 1 a 2 fun1.sh 3
1 a 2 fun1.sh 3
[root@tianqi-02 shell]#
函数,能够直接写在脚本内,至关于直接调用
[root@tianqi-02 shell]# vim fun1.sh
#!/bin/bash
input(){
echo $1 $2 $3 $0 $#
}
input $1 $2 $3
[root@tianqi-02 shell]# sh fun1.sh 1 2 4
1 2 4 fun1.sh 3
[root@tianqi-02 shell]#
[root@tianqi-01 shell]# vim fun2.sh
#!/bin/bash
sum() {
s=$[$1+$2]
#定义变量s = $1+$2 /其中 $1为第一个参数,$2为第二个参数
echo $s
}
sum 1 2
#输出第一个参数和第二个参数
[root@tianqi-01 shell]# sh fun2.sh
3
[root@tianqi-01 shell]#
[root@tianqi-01 shell]# sh -x fun2.sh
+ sum 1 2
+ s=3
+ echo 3
3
[root@tianqi-01 shell]#
[root@tianqi-02 shell]# vim fun3.sh
#!/bin/bash
ip() {
ifconfig |grep -A1 "$1: " |awk '/inet/ {print $2}'
#查看网卡,过滤出参数1及下面的一行,匹配inet行并打印出第二段
}
read -p "Please input the eth name: " e
myip=`ip $e`
echo "$e address is $myip"
[root@tianqi-02 shell]# ifconfig |grep -A1 "eno16777736"
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.23.129 netmask 255.255.255.0 broadcast 192.168.23.255
[root@tianqi-02 shell]#
[root@tianqi-02 shell]# vim ip1.sh
#!/bin/bash
#coding:utf8
ip()
{
a=`ifconfig |grep -A1 "$1 " |tail -1 |awk -F" " '{print $2}'`
if [ -z "$a" ]
then
echo $1
echo "没有这个网卡名"
exit 1
fi
b=`echo $a |grep addr`
if [ -z "$b" ]
then
echo $1
echo "此网卡没有IP地址"
exit 1
fi
echo $a
}
read -p "请输入你的网卡名字: " eth
ip $eth
20.18shell中的数组
seq 1 5
)1.数组,就是一串字符串或者一串数字,造成的一个变量,把这个变量叫作数组
[root@tianqi-02 ~]# b=(1 2 3) //定义数组
[root@tianqi-02 ~]# echo ${b[@]} //表示数组
1 2 3
[root@tianqi-02 ~]# echo ${b[*]} //表示数组
1 2 3
[root@tianqi-02 ~]#
2.查看某一个元素的值
[root@tianqi-02 ~]# echo ${b[1]}
2
[root@tianqi-02 ~]# echo ${b[2]}
3
[root@tianqi-02 ~]# echo ${b[0]}
1
[root@tianqi-02 ~]#
3.获取数组元素的个数
[root@tianqi-02 ~]# echo ${#b[@]}
3
[root@tianqi-02 ~]#
[root@tianqi-02 ~]# b[3]=a
[root@tianqi-02 ~]# echo ${b[*]}
1 2 3 a
[root@tianqi-02 ~]# b[3]=aaa
[root@tianqi-02 ~]# echo ${b[*]}
1 2 3 aaa
[root@tianqi-02 ~]#
[root@tianqi-02 ~]# unset b[3]
[root@tianqi-02 ~]# echo ${b[*]}
1 2 3
[root@tianqi-02 ~]# unset b //把数组的值清空
[root@tianqi-02 ~]# echo ${b[*]}
[root@tianqi-02 ~]#
[root@tianqi-02 ~]# a=(`seq 1 10`)
[root@tianqi-02 ~]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
[root@tianqi-02 ~]#
[root@tianqi-02 ~]# echo ${a[*]:3:4}
4 5 6 7
[root@tianqi-02 ~]#
[root@tianqi-02 ~]# echo ${a[*]:0-3:2}
8 9
[root@tianqi-02 ~]# echo ${a[*]:7:2}
8 9
[root@tianqi-02 ~]#
[root@tianqi-02 ~]# echo ${a[*]/8/6}
1 2 3 4 5 6 7 6 9 10
[root@tianqi-02 ~]#
[root@tianqi-02 ~]# a=(${a[*]/8/6})
[root@tianqi-02 ~]# echo ${a[*]}
1 2 3 4 5 6 7 6 9 10
[root@tianqi-02
20.19告警系统需求分析
友情连接:阿铭linux