1. 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 this
The name of a small unit.
Format: function F_name () {
Commond
}
function must be placed at the front of the script
Case 1:
#!/bin/bash
Input () {
echo "First parameter is $"
echo "the second parameter is $"
echo "Total number of parameters $# "
echo" This script has a name of $ "
}
read-p" input a num: "N /span>
read-p "input a num:" M
input $n $m
Case 2:
#!/bin/bash
Input () {
echo "The first parameter is $"
echo "The second parameter is $"
echo "Total number of parameters $#"
echo "This script's name is $"
}
Input $ $
Operations in the function:
Case 1:
#!/bin/bash
Sum () {
s=$[$1+$2]
Echo $s
}
sum $
Case 2:
#!/bin/bash
Sum () {
S=$[$1+$2+$3]
Echo $s
}
Read-p "AAA:" A
Read-p "BBB:" b
Read-p "CCC:" C
Sum $a $b $c
IP Display function (it took a long time to integrate all of them, especially when entering numbers, the corresponding NIC and IP are displayed):
#!/bin/bash
#by Yumingzhen
#
echo ""
k=0
For i in ' ifconfig | awk '/:/ {print ' |grep ': ' |sed ' s/://g '
Do
z= $i
k=$[$k +1]
m= $k
echo $m = $i
cc= $cc $i:
Done
echo "Press a show all IP addr"
echo "Press q|quit| Q Exit "
Ip_fun () {
ifconfig |grep-a1 "$id" | awk '/inet/{print $} '
}
While :
Do
echo ""
read-p "input a num to get Ip_add:" N
end= ' ifconfig | awk '/:/ {print ' |grep ': ' |wc-l '
n1= ' echo $n |sed ' s/[0-9]//g '
if [$n = = "Q"] | | [$n = = "Quit"] | | [$n = = "Q"]; Then
Exit 0
elif [$n = = "a"];then
For A in ' seq 1 $end '
Do
id= ' echo $cc |cut-d ': '-f $a '
echo $id = ' Ip_fun '
Done
Exit 0
Elif [-N "$n 1"] | | [-Z "$n"] | | ["$n"-gt "$end"];then
echo "Please enter the correct number, not greater than $end"
Continue
fi
Id= ' echo $cc |cut-d ': '-f $n '
echo ""
echo $id ip= ' Ip_fun '
Done
2. Arrays
@ or * number
Define 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, 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]
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})
3, Alarm system main script
#!/bin/bash
#Written by aming.
# whether to send mail switch
Export send=1
# filter IP address
export addr= '/sbin/ifconfig |grep-a1 ' ens33: ' |awk '/inet/{print $} '
dir= ' pwd '
# requires only the last level of directory name
last_dir= ' echo $dir |awk-f '/' {print $NF} '
# The purpose of this is to ensure that we are in the bin directory when the script is executed, otherwise the monitoring scripts, mails, and logs may not be found
if [$last _dir = = "Bin"] | | [$last _dir = = "bin/"]; Then
conf_file= ". /conf/mon.conf "
Else
echo "You shoud cd bin dir"
Exit
fi
exec 1>>. /log/mon.log 2>> /log/err.log
echo "' Date +"%F%T "' Load average '
/bin/bash. /shares/load.sh
#先检查配置文件中是否需要监控502
if Grep-q ' to_mon_502=1 ' $conf _file; Then
export log= ' grep ' logfile= ' $conf _file |awk-f ' = ' {print $} ' |sed ' s///g '
/bin/bash. /shares/502.sh
fi
Functions, arrays, alarm system scripts in the shell