70. Function in shell, array in shell, demand analysis of alarm system
I. 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. A function is a child shell, a code snippet.
Functions in the shell must be defined at the top of the script.
Format: function F_name () {
Command
} function must be placed at the front
F_name: function name, it is best not to conflict with some key words in the shell, such as for.
Command: Specific commands
The word function can be omitted.
Example 1
#!//bin/bash
function InP () {
echo "The first par is $"
echo "The Serond par is $"
echo "The third par is $"
echo "The SCRITP name is $"
echo "The number of par is $#"
}
INP a B 2 3 ABC//How to invoke the above function, direct input, and then add the function name, it is equal to call it, like a command in the shell
# sh fun1.sh
The first par is a
The second par is b
The third par is 2
The SCRITP name is fun1.sh
The number of par is 5
You can also put the parameters outside
# Vim Fun1.sh
#!//bin/bash
function InP () {
echo "The first par is $"
echo "The second par is $"
echo "The third par is $"
echo "The SCRITP name is $"
echo "The number of par is $#"
}
INP $ zero//The first parameter of fun1.sh is represented here
# sh fun1.sh 1 with only one 1 behind
The first par is 1
The second par is both empty
The third par is
The SCRITP name is fun1.sh
The number of par is 1 parameter is 1
Example 2
# Vim Fun2.sh
#!/bin/bash
Sum () {
S=$[$1+$2]
Echo $s
}
Sum 1 2
# sh-x Fun2.sh
+ SUM 1 2//Is $1+$2,$1=1,$2=2.
+ s=3
+ Echo 3
3
Example 3
#!/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"
Analytical:
ifconfig |grep-a1 "$:" | tail-1 | awk ' {print $} ' | awk-f ': ' {print $} '
Here's the script to write $:, this colon, is critical, because ifconfig out the NIC name has a colon.
# ifconfig |GREP-A1 "ENS33:"//Command line demonstration of these three commands:
Ens33:flags=4163<up,broadcast,running,multicast> MTU 1500
inet 192.168.93.130 netmask 255.255.255.0 broadcast 192.168.93.255
# ifconfig |GREP-A1 "ENS33:" |TAIL-1//Here tail-1 can also be replaced with grep ' inet '
inet 192.168.93.130 netmask 255.255.255.0 broadcast 192.168.93.255
# ifconfig |GREP-A1 "ens33:" |tail-1 |awk ' {print $} '
192.168.93.130
# sh-x fun3.sh Test:
+ read-p ' Please input the ETH name: ' ETH
Please input the ETH name:ens33
+ + IP Ens33
+ + Ifconfig
+ + grep-a1 ' ens33: '
+ + TAIL-1
+ + awk ' {print $} '
+ myip=192.168.93.130
+ echo ' ENS33 address is 192.168.93.130 '
ENS33 address is 192.168.93.130
Ii. Arrays in the shell
A String string or a bunch of Digital , a variable is formed, and the variable is called an array. You can manipulate the array, or you can take one of the values in the array.
Array 1:
Define array a= (1 2 3 4 5)
Printed format: Echo ${a[@]}, the inside of the @ symbol can also be changed to *
echo ${#a[@]} Gets the number of elements in the array
Echo ${a[2]} reads the value of an element starting from 0:0,1,2,3,4,5; the number in square brackets indicates its subscript. (the element is the first position)
echo ${a[*]} equals ${a[@]} displays the entire array
Array Assignment:
element assignment, change, overwrite, replace
a[3]=100
Echo ${a[@]}
a[5]=2
echo ${a[@]}//If the subscript does not exist, an element will be added automatically
Deletion of arrays
Unset a deletes the entire array
unset a[1] Delete the elements in the array
Array 2:
Array shards
A= (' seq 1 10 ') array assignment can use SEQ
Echo ${a[@]:0:3} starts with the No. 0 element and intercepts how many
Echo ${a[@]:1:4} For example, start with a second element and intercept 4
Echo ${a[@]:0-3:2} starts with the bottom 3rd element (0-3) and intercepts 2
Array substitution:
Echo ${a[@]/3/99}//echo Replace, replace 3 with 99
a= (${a[@]/3/99}) //Direct assignment, enclosed in parentheses, is critical
Three, the alarm system needs analysis
Shell Project-Alarm system
Requirements: Use the shell to customize a variety of personalized alarm tools, but the need for unified management, standardized management.
Idea: Specify a script package that contains the main program, subroutine, configuration file, mail engine, output log, and so on.
Main program: As the entire script portal, is the lifeblood of the entire system. Execute the main program directly when executing the alarm system, write a script that calls these functions.
Configuration file: is a control center that uses it to switch individual subroutines, specifying each associated log file.
Subroutine: This is the real monitoring script, used to monitor each indicator. Does not need to be executed directly, it is called by the main program. While,for, the monitoring system load is placed in the sub-program.
Mail Engine: It is implemented by a Python program, it can define the mail server, sender and sender password, call third-party mail, such as 163.
Output log: The entire monitoring system should have a log output.
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:
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.
70. Function in shell, array in shell, demand analysis of alarm system