2018-4-20 17 weeks 3 lessons shell function, array, alarm demand analysis

20.16/20.17 Functions in the shell


·A function is to organize a piece of code into a small unit, and give this small unit a name. When using this code, you can directly call the name of this small unit.

Format: function f_name() { 

                        command

         }

function must be first


·Example 1

#!/bin/bash
input() { ##Define f_name best not to conflict with keywords in shell
    echo $1 $2 $# $0
}
input 1 a b

$1 $2 first, second parameter

$ # Screenplay name

$0 number of parameters


[root@localhost shell]# sh -x fun1.sh
+ input 1 a b
+ echo 1 to 3 fun1.sh
1 a 3 fun1.sh

You can also write parameters outside the script

1.png

[

root@localhost shell]# sh -x fun1.sh 1 a d ##Parameters are written outside the script, after the name, also OK
+ input 1 a d
+ echo 1 to 3 fun1.sh
1 a 3 fun1.sh


Example 2

#!/bin/bash
sum() {
    s=$[$1+$2]
    echo $s
}
sum 1 2

2.png

[root@localhost shell]# sh -x fun2.sh
+ sum 1 2
+ s=3
+ echo 3
3


As in example 1, the external call parameters can be

3.png

[root@localhost shell]# sh -x fun2.sh 3 4
+ sum 3 4
+ s=7
+ echo 7
7
[root@localhost shell]# sh -x fun2.sh 3421 4352
+ sum 3421 4352
+ s=7773
+ echo 7773
7773

Which function you want to call, define the function just before the call statement


Example 3

#!/bin/bash
ip() {
    ifconfig |grep -A1 "$1" |tail -1 |awk '{print $2}'|awk -F':' '{print $2}'
}
read -p "Please input the eth name: " e
myip=`ip $e`
echo "$e address is $myip"

4.png

The ip can also be obtained by writing the following:

ifconfig | grep -A1 "$1: " | awk '/inet/ {print $2}'


·after class homework:

Enter the name of the network card to determine whether it is empty or not, whether it is a network card in the system

5.png

Idea: first solve the problem of empty input, if the input content is empty, prompt to enter the content and recycle,

Secondly, if the network card exists in the system, and the network card configuration file is under /etc/sysconfig/network-scripts/, and all start with ifcfg-, then it can be judged that the input network card name is "ifcfg-network card name" "Whether the file exists, if it exists, the next operation is allowed, otherwise the cycle is repeated





20.18 Arrays in the shell


·Define the array a=(1 2 3 4 5); echo ${a[@]}The  array does not have to be numbers

[root@localhost shell]# a=(1 2 3 4 5)
[root@localhost shell]# echo ${a[@]}
1 2 3 4 5
[root@localhost shell]# echo ${a[*]}
1 2 3 4 5


· echo ${#a[@]} Get the number of elements in the array

[root@localhost shell]# a=(1 2 3 4 5)
[root@localhost shell]# echo ${a[*]}
1 2 3 4 5
[root@localhost shell]# echo ${#a[*]}
5


echo ${a[2]} to read the third element, the array starts at 0

[root@localhost shell]# echo ${a[0]}
1
[root@localhost shell]# echo ${a[1]}
2
[root@localhost shell]# echo ${a[2]}
3


echo ${a[*]} is equivalent to ${a[@]} to display the entire array

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


· Array assignment

·Add elements

a[5]=100; echo ${a[@]}

[root@localhost shell]# a[5]=100
[root@localhost shell]# echo ${a[*]}
1 2 3 4 5 100
[root@localhost shell]# a[5]=aaa
[root@localhost shell]# echo ${a[*]}
1 2 3 4 5 aaa

a[6]=bbb; echo ${a[@]} will automatically add an element if the subscript does not exist

[root@localhost shell]# a[6]=bbb
[root@localhost shell]# echo ${a[*]}
1 2 3 4 5 aaa bbb

Array deletion

·unset a; unset a[1]

[root@localhost shell]# unset a[5] ##Delete the 6th value
[root@localhost shell]# echo ${a[*]}
1 2 3 4 5 bbb
[root@localhost shell]# unset a[5] ## Delete the 6th element again, there is no change, so the subscript will not change
[root@localhost shell]# echo ${a[*]}
1 2 3 4 5 bbb
[root@localhost shell]# unset a[6] ##The subscript has not changed, it is still the 7th element deleted
[root@localhost shell]# echo ${a[*]}
1 2 3 4 5
[root@localhost shell]# unset a ##Empty the array
[root@localhost shell]# echo ${a[*]}


Array sharding

a=(`seq 1 10`)

echo ${a[@]:0:3} starts from the first element, intercepts 3

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


echo ${a[@]:1:4} starts from the second element, intercepts 4

[root@localhost shell]# echo ${a[*]:1:4}
2 3 4 5


echo ${a[@]:0-3:2} starts from the 3rd last element, intercepts 2

[root@localhost shell]# echo ${a[@]:0-3:2}
8 9


Array replacement

echo ${a[@]/3/100} replaces the value

a=(${a[@]/3/100})

[root@localhost shell]# echo ${a[*]/3/100}
1 2 100 4 5 6 7 8 9 10
[root@localhost shell]# a=(${a[*]/3/100})
[root@localhost shell]# echo ${a[*]}
1 2 100 4 5 6 7 8 9 10




20.19 Analysis of Alarm System Requirements


Requirements: Use the shell to customize various personalized alarm tools, but need unified management and standardized management.

· Ideas: Specify a script package, including the main program, subprograms, configuration files, mail engine, output log, etc.

·Main program: As the entrance of the whole script, it is the lifeblood of the whole system.

·Configuration file: It is a control center, which is used to switch each subprogram and specify each associated log file.

·Subroutine: This is the real monitoring script, which is used to monitor various indicators.

Mail engine: It is implemented by a python program, which can define the server, sender and sender password for sending mail

·Output log: The entire monitoring system must have log output.


Requirements: Our machines have various roles, but the same monitoring system must be deployed on all machines, which means that no matter what role all machines are, the entire program framework is the same. The difference lies in the customization of different roles according to different roles. configuration file.


Program Architecture:                       

6.png

Under bin is the main program

conf is the configuration file

Under shares are the various monitoring scripts

Mail is the mail engine

The log is the log


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324619008&siteId=291194637