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.
Format:
function f_name() { command}
Note: The function must be placed at the front! Functions can omit the direct write function name.
1. Show columns: Print shell parameters, function input ()
#!/bin/bashinput() {? ? echo $1 $2 $# $0}input 1 a b
$ $ for the first argument, and $ A for the file name $ #表示参数的个数
Edit script:
Execution results
An INP can also be followed by a variable name:
Execution Result:
2. Example: function sum () to define addition
#!/bin/bashsum() {? ? s=$[$1+$2]? ? echo $s}sum 1 2
3. Example: Displays the IP address of the network card
#!/bin/baship() {? ? ifconfig |grep -A1 "$1 " |tail -1 |awk ‘{print $2}‘|awk -F‘:‘ ‘{print $2}‘}read -p "Please input the eth name: " emyip=`ip $e`echo "$e address is $myip"
Arrays in the shell
The so-called array, which is a string of numbers or strings of variables that it forms, we call this variable an array.
Useful, but not many arrays are used.
1. Defining arrays
Format:
A= (1 2 3 4 5); Echo ${a[@]} The numbers here can also be written as strings
echo ${#a [@]} Gets the number of elements in the array
echo ${a[2]} reads the third element, array starting from 0
echo ${a[*]} equals ${a[@]} displays the entire array
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
Uset A; Unset A[1]
For example:
Defines the array b= (a string of numbers or strings);
Show results echo ${b[]}
[Email protected] shell]# b= (1 2 3)
[[email protected] shell]# echo ${b[]}
1 2 3
We can also view the value of an element
For example:
We want to see the value of an element
Echo ${a[2]}
[[email protected] shell]# echo ${b[2]}
3
Here is a special, array starting from 0, that is, 0 is the first element.
We can also display the number of elements
echo ${#b []} Gets the number of elements in the array, #表示元素个数.
For example:
[[email protected] shell]# echo ${#b []}
3
Array Assignment
We can also assign values to one of the elements of an array.
[Email protected] shell]# B[3]=a
[[email protected] shell]# echo ${b[]}
1 2 3 A
We have added a new element A in this, directly after the printing time, this is because the subscript does not exist will automatically add an element
can also be replaced
[Email protected] shell]# B[0]=AAA
[[email protected] shell]# echo ${b[]}
AAA 2 3 A
We can also delete the value of the array, using the unset command
[[email protected] shell]# echo ${b[]}
AAA 2 3 A
[Email protected] shell]# unset b[3]
[[email protected] shell]# echo ${b[]}
AAA 2 3
Array shards
Sometimes we want to intercept part of the array
[[email protected] shell]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
For example
We're going to intercept from 4 to 7.
4 5 6 7
Intercept from behind
8 9
What we need to note here is that the interception from behind must be written in 0-or else it will not succeed.
Substitution of arrays
[[email protected] shell]# echo ${A[]/8/6}
1 2 3 4 5 6 7 6 9 10
We can also assign values directly
[[email protected] shell]# echo ${a[]/3/100}
1 2 100 4 5 6 7 8 9 10
[[email protected] shell]# echo ${A[*]/100/3}
1 2 3 4 5 6 7 8 9 10
Three, the alarm system needs analysis
1, demand: Use Shell to customize a variety of personalized alarm tools, but the need for unified management, standardized management.
2. Idea: Specify a script package that contains the main program, subroutine, configuration file, mail engine, output log, and so on.
3, the main program: as the entire script entrance, is the lifeblood of the entire system.
4, configuration file: is a control center, use it to switch the various sub-programs, specify 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 to have log output
function in shell, array in shell, demand analysis of alarm system
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:
Analysis of function, array and alarm system requirement in shell