2018-06-01linux Learning

Source: Internet
Author: User

20.16-17 functions in the shell

The function is to organize a piece of code into a small unit, and give the small unit a name, when the code is used to call the name of the small unit directly.

Format: function F_name () {
Command
}
The function has to be on the front

Example 1
#!/bin/bash
Input () {
? ? echo $ $#
}

Input 1 A B

Example 2
#!/bin/bash
Sum () {
? ? S=$[$1+$2]
? ? Echo $s
}
Sum 1 2

Example 3
#!/bin/bash
IP () {
? ? Ifconfig |grep-a1 "$" |tail-1 |awk ' {print $} ' |awk-f ': ' {print $} '
}
Read-p "Please input the ETH name:" E
myip=ip $e
echo "$e address is $myip"

Operation Process

Case 1

[Email protected] shell]# vim fun1.sh

#!/bin/bash
function InP () {
Echo $ $ $ $#
}
INP 1 a 2

[[email protected] shell]# sh fun1.sh 1 a 2 fun1.sh 3

[Email protected] shell]# vim fun1.sh

#!/bin/bash
function InP () {
echo "The first par is $"
echo "The second par is $"
echo "The third par is $"
echo "The scrip name par is $"
echo "The number of par is $#"
}
INP b a 5 w z x

[[email protected] shell]# sh fun1.sh The first par is bThe second par is aThe third par is 5The scrip name par is fun1.shThe number of par is 6

[Email protected] shell]# vim fun1.sh

#!/bin/bash
function InP () {
echo "The first par is $"
echo "The second par is $"
echo "The third par is $"
echo "The scrip name par is $"
echo "The number of par is $#"
}
INP $ $4 $ $6

[[email protected] shell]# sh fun1.sh 6 wThe first par is 6The second par is wThe third par is The scrip name par is fun1.shThe number of par is 2[[email protected] shell]# sh fun1.sh 6 w d a f e g qThe first par is 6The second par is wThe third par is dThe scrip name par is fun1.shThe number of par is 6

Case 2

[Email protected] shell]# vim fun2.sh

#!/bin/bash
Sum () {
S=$[$1+$2]
Echo $s
}
Sum 1 2

[[email protected] shell]# sh fun2.sh 3

Case 3

[[email protected] shell]# ifconfig| grep -A1 "ens33: "| tail -1| awk ‘{print $2}‘192.168.106.160[[email protected] shell]# ifconfig| grep -A1 "ens33: "| tail -1| awk ‘/inet/ {print $2}‘192.168.106.160

[Email protected] shell]# vim fun3.sh

#!/bin/bash
IP () {
Ifconfig| GREP-A1 "$:" | tail-1| awk ' {print $} '
}
Read-p "Please input the ETH name:" ETH
myip=ip $eth
echo "$eth address is $myip"

[[email protected] shell]# sh fun3.sh Please input the eth name: ens33ens33 address is 192.168.106.160[[email protected] shell]# sh fun3.sh Please input the eth name: ens37ens37 address is 172.16.166.130

20.18 Arrays in the shell

Arrays in the 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]

Arrays in the 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})

Operation Process

Defining arrays

[[email protected] shell]# b=(1 2 3)[[email protected] shell]# echo $b1[[email protected] shell]# echo ${b[@]}1 2 3[[email protected] shell]# echo ${b[*]}1 2 3[[email protected] shell]# echo ${b[0]}1[[email protected] shell]# echo ${b[1]}2[[email protected] shell]# echo ${b[2]}3[[email protected] shell]# echo ${#b[*]}3

Array Assignment

[[email protected] shell]# b[3]=a[[email protected] shell]# echo ${b[*]}1 2 3 a[[email protected] shell]# b[3]=aaa[[email protected] shell]# echo ${b[*]}1 2 3 aaa[[email protected] shell]# unset b[3][[email protected] shell]# echo ${b[*]}1 2 3[[email protected] shell]# unset b[[email protected] shell]# echo ${b[*]}

Array shards

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

20.19 Alarm system Requirements Analysis

Shell Project-Alarm system

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

Requirements: Our machine roles are varied, but the same monitoring system is deployed on all machines, and the entire program framework is consistent, regardless of the role of all machines, depending on the role of the different configuration files.

Program architecture:??

(Main directory 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]

Under Bin is the main program
Conf is the configuration file
Shares is the various monitoring scripts
Mail engine under Mail
Log is the journal.

2018-06-01linux Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.