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? ?? ?? ?? ? }函数必须要放在最前面
- Script Example 1 (used to define function printing parameters)
[[email protected] aming]# vim fun1.sh[[email protected] aming]# cat fun1.sh #!/bin/bashfunction INP () {echo $ $#}INP 1 a 2[[email protected] aming]# sh fun1.sh 1 a 2 fun1.sh 3[[email protected] aming]# sh-x Fu n1.sh + INP 1 a 1 a 2 fun1.sh a 2 fun1.sh 3[[email protected] aming]# vi fun1.sh [[email protected] A ming]# Cat fun1.sh #!/bin/bashfunction INP () {echo "The first par is $" echo "the second par is" echo "the TH IRD par is $ "echo" the SCRITP name is $ "echo" the number of Par is $# "}inp b a 2 3 adf[[email protected] am ing]# sh fun1.sh The first par is bthe second par are athe third par is 2the scritp name was fun1.shthe number of par is 5[[ Email protected] aming]# sh-x fun1.sh + INP b a 2 3 adf+ echo ' The first par is B ' the first par is + + echo ' the Seco Nd par is a ' the second par is a + echo ' the third par is 2 ' the third par is the "the SCRITP name is" fun1.sh ' the SCRITP Name is fun1.sh+ Echo ' tHe number of par is 5 ' the number of par is 5[[email protected] aming]#
- Script Example 2 (function to define an addition)
[[email protected] aming]# vim fun2.sh[[email protected] aming]# cat fun2.sh #!/bin/bashsum() { s=$[$1+$2]
- Script Example 3 (used to define the display IP)
[[email protected] aming]# vim fun3.sh [[email protected] aming]# cat fun3.sh #!/bin/baship(){ ifconfig |grep -A1 "$1: "|awk ‘/inet/ {print $2}‘}read -p "Please input the eth name: " ethip $eth[[email protected] aming]# sh fun3.sh Please input the eth name: ens33172.16.111.100[[email protected] aming]# sh -x fun3.sh + read -p ‘Please input the eth name: ‘ ethPlease input the eth name: ens33+ ip ens33+ grep -A1 ‘ens33: ‘+ awk ‘/inet/ {print $2}‘+ ifconfig172.16.111.100
Array 1 in the shell in array 1.shell
- defines the array a= (1 2 3 4 5); Echo ${a[@]}
- echo ${#a [@]} Gets the number of elements in the array
- echo ${a[2]} reads the third element, with the array starting from 0
- echo ${a[*]} equals ${a[@]} displays the entire array
array assignment
- a[1]=100; Echo ${a[@]}
- a[5]=2; echo ${a[@]} if the subscript does not exist, it will automatically add a Delete element
Array
- unset A; unset a[1]
[[email protected] aming]# b=(1 2 3)[[email protected] aming]# echo ${b[@]}1 2 3[[email protected] aming]# echo ${b[*]}1 2 3[[email protected] aming]# echo ${b[1]}2[[email protected] aming]# echo ${b[2]}3[[email protected] aming]# echo ${b[0]}1[[email protected] aming]# echo ${#b[@]}3[[email protected] aming]# b[3]=a[[email protected] aming]# echo ${b[*]}1 2 3 a[[email protected] aming]# b[3]=aaa[[email protected] aming]# echo ${b[*]}1 2 3 aaa[[email protected] aming]# unset b[3]
Arrays in 2.shell 2
- 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})
Alarm system Requirements Analysis 1.shell Project-Alarm system
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 of shell, demand analysis of alarm system