Functions in 20.16 20.17shell (upper and lower); 20.18
Functions in 20.16 shell (part I)
The function is to organize a piece of code into a small unit and give the small unit
A name, and the name of the small unit can be called directly when using this code.
1.
[root@hao-01 ~] # vi fun1.sh
Add content:
#! / bin/bash
Function inp () {
Echo "The first par is $1"
Echo "The second par is $2"
Echo "The third par is $3"
Echo "the scritp name is $0"
Echo "the number of par is $#"
}
Inp $1 $2 $3
two。 Execute the fun1.sh script, followed by the function:
[root@hao-01 ~] # sh fun1.sh 1
Functions in 20.17 shell (part two)
1. Addition function:
[root@hao-01 ~] # vi fun2.sh
Add content:
#! / bin/bash
Sum () {
Slots $[$1mm / 2]
Echo $s
}
Sum 1 10
two。 Execute the fun2.sh script:
[root@hao-01 ~] # sh-x fun2.sh
1. Enter the name of the network card and display the network card ip:
[root@hao-01 ~] # vi fun3.sh
Add content:
#! / bin/bash
Ip ()
{
Ifconfig | grep-A1 "$1:" | awk'/ inet/ {print $2}'
}
Read-p "please input the eth name:" ech
Ip $eth
two。 Execute the fun3.sh script:
[root@hao-01 ~] # sh fun3.sh
Please input the eth name: ens33
Arrays in 20.18 shell
1. Define an array:
[root@hao-01] # a = (1 2 3 4 5)
two。 View the elements of array a:
[root@hao-01 ~] # echo ${a [*]}
3. View the value of an element in the array (the array starts at 0 and the value is 1):
[root@hao-01 ~] # echo ${a [1]}
4. Gets the number of elements in the array:
[root@hao-01 ~] # echo ${# a [*]}
5. If the subscript does not exist, an element is automatically added:
[root@hao-01 ~] # a [5] = b
[root@hao-01 ~] # echo ${a [*]}
Array element assignment (change replacement):
[root@hao-01 ~] # a [5] = bbb
[root@hao-01 ~] # echo ${a [*]}
6. Delete array elements:
7. Delete (empty) array values:
[root@hao-01 ~] # unset a
[root@hao-01 ~] # echo ${a [*]}
8. Set the array:
[root@hao-01 ~] # a = (`seq 1 10`)
[root@hao-01 ~] # echo ${a [*]}
9. Starting with the first element, five values are intercepted:
[root@hao-01 ~] # echo ${a [*]: 0:5}
Starting with the second element, five values are intercepted:
[root@hao-01 ~] # echo ${a [*]: 1:5}
10. Starting with the penultimate element, intercept 2 values:
[root@hao-01 ~] # echo ${a [*]: 0-3:2}
11. Intercept and replace, print 8 elements as cc66:
[root@hao-01 ~] # echo ${a [@] / 8/cc66}
twelve。 Replace the element value, and replace 8 elements with cc66:
[root@hao-01 ~] # a = (${a [*] / 8/cc66})
[root@hao-01 ~] # echo ${a [*]}
Replace the element value and replace the cc66 element with 888:
[root@hao-01 ~] # a = (${a [*] / cc66/888})
[root@hao-01 ~] # echo ${a [*]}
20.19 alarm system requirement Analysis
1. Requirements: use shell to customize a variety of personalized alarm tools, but need unified management and standardized management.
two。 Idea: specify a script package that contains main programs, subroutines, configuration files, mail engines, output logs, and so on.
3. Main program: as the entrance of the whole script, it is the lifeblood of the whole system.
4. Configuration file: is a control center that switches on and off subroutines and specifies each associated log file.
5. Subroutine: this is the real monitoring script, which is used to monitor various indicators.
6. Mail engine: is implemented by a python program that defines the server, sender and sender password for sending mail.
7. Output log: the whole monitoring system should have log output.