20.16-20.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.
格式: function f_name() { command }函数必须要放在最前面
#!/bin/bashfunction inp(){ echo $1 $2 $3 $0 $#}inp 1 a 2
#!/bin/bashsum() { s=$[$1+$2] echo $s}sum 1 2
#!/bin/baship(){ ifconfig |grep -A1 "$1: "|awk ‘/inet/ {print $2}‘}read -p "Please input the eth name: " ethip $eth
20.18 Arrays in the shell
Array Assignment
- a[1]=100; Echo ${a[@]}
- a[5]=2; Echo ${a[@]} An element is added automatically if the subscript does not exist
Deletion of arrays
- Unset A; Unset A[1]
Array shards
- A= (seq 1 5)
- Echo ${a[@]:0:3} starts with the first element and intercepts 3
- Echo ${a[@]:1:4} starts with the second element and intercepts 4
- Echo ${a[@]:0-3:2} Starts from the bottom 3rd element and intercepts 2
Array substitution
- Echo ${a[@]/3/100}
- A= (${a[@]/3/100})
20.19 Alarm system Requirements Analysis
- 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.
- 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.
- Mail Engine: It is implemented by a Python program that defines the server to which the message is sent, the person who sent it, and the sender's password
- 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 the bin is the main program;
Conf is the configuration file;
Shares is the various monitoring scripts;
Mail engine under mail;
Log is the journal.
function in shell, array in shell, demand analysis of alarm system