If one, if base 1, single branch 1.1 syntax
if语句语法 单分支结构语法: if [条件]; then 指令 fi 或 if [条件] then 指令 fi
1.2 Examples
[[email protected] day4]# cat if-single1.sh #!/bin/sh#功能:单分支if结构整数比较,用-lt格式例子#created by xsz#date:20180107if [ 10 -lt 12 ] then echo "Yes,10 is less than 12"fi[[email protected] day4]# sh if-single1.sh Yes,10 is less than 12传参方方式:[[email protected] day4]# cat if02.sh #/bin/shif [ $1 -lt $2 ] then echo "Year,$1 is less than $2"firead接收方式:[[email protected] day4]# cat if03.sh #/bin/shread -t 10 -p "please input two number:" num1 num2if [ $num1 -lt $num2 ] then echo "Year,$num1 is less than $num2"fi以上脚本问题1、无法完整比较整数大小2、没有对参数的个数以及变量内容做判断
1.3 Example 3
Example three (think): The development script implementation if the/server/scripts below exists if3.sh the output to the screen.
Note: If the if3.sh does not exist after the script is executed, the if3.sh script is created automatically.
我的脚本:#!/bin/bashi=/server/scripts/if3.sh[ -e $i ] && cat $i && exit 0if [ $(sh $i 2> /dev/null;echo $?) != 0 ] then echo "$i is no exist,touching if03.sh..." echo "I am if03.sh" > $ifi老男孩的脚本1:#!/bin/shdir=/server/scripts[ ! -d $dir ] && mkdir -p $dirfile="if3.sh"if [ -f "$dir/$file" ]; then echo "$file is exist." exit 0fitouch $dir/$file同学:[[email protected] day4]# cat tiaojian.sh #!/bin/shdir=/server/scriptsfile=if3.shcd $dir || mkdir -p $dir [ ! -f "$file" ] && touch $file || echo $file
1.4 Memory Alarm Script
Development script to determine the size of the system memory, if less than 100M mail alarm. The test alarm is successfully added to the system scheduled task every 3 minutes to perform a check. Common ways to send mail: [[[email protected] day4]# mail-s "tittle" [email protected] </etc/hosts[[email protected ] day4]# echo "Oldboy" | Mail-s "Tittle" [email protected] installation email: Yum install-y sendmailsendmail boot slow cause 1, hostname not configured [[email protected] day4]# uname-nmaster4.com10.201.106.134 master4 master4.com[[email protected] day4]# NETSTAT-TANP | grep TCP 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 15622/sendmail Answer: Focus on the problem solving process 1 And then command line to remove the condition (mine is CentOS7) [[email protected] day4]# free-m | grep Mem | awk ' {print $NF} ' 1222, scripting [[email protected] day4]# vim if04.sh#!/bin/shcurrent_mem= ' free-m | grep Mem | awk ' {print $NF} ' mem_chars= ' Our sys mem is: $current _mem "If [$current _mem-lt];then echo" $mem _chars "| Mail-s "Tittle" [email protected]fi[[email protected] day4]# sh-x if04.sh + awk ' {print $NF} ' + + grep mem++ fr ee-m+ current_mem=178+ mem_chars= ' OurSYS mem is:178 ' + ' [' 178-lt + '] ' + mail-s tittle [email protected]+ echo ' Our sys mem is:178 ' Join to timed tasks: [[email  ;p rotected] day4]# crontab-l#monitor MEMORY*/3 * * * */bin/sh/server/scripts/day4/if04.sh >/dev/null 2>&1
2, Dual Branch 2.1 compare two integer size (available in lowercase or greater than or equal)
我的脚本:[[email protected] day4]# cat if05.sh #!/bin/shif [ $1 -le $2 ];then if [ $1 -lt $2 ];then echo "$1 is less than $2" else echo "$1 is eq $2" fielse echo "$1 is big than $2"fi[[email protected] day4]# cat if-double01.sh #!/bin/shif [ $1 -lt $2 ];then echo "$1 < $2"else echo "$1 >= $2"fi[[email protected] day4]# sh if-double01.sh 4 54 < 5[[email protected] day4]# sh if-double01.sh 2 22 >= 2[[email protected] day4]# sh if-double01.sh 2 12 >= 1
3, multi-branch 3.1 or compare two integer size
[[email protected] day4]# cat if-double02.sh #!/bin/shread-p "Please input first number:" Num1read-p " Second number: "Num2if [$num 1-lt $num 2];then echo" $num 1 < $num 2 "elif [$num 1-gt $num 2];then echo" $num 1 ; $num 2 "Else echo" $num 1 = $num 2 "fi[[email protected] day4]# sh if-double02.sh Please input first number:4please INP UT second number:34 > 3[[email protected] day4]# sh if-double02.sh Please input first number:4please input second number:44 = 4[[email protected] day4]# sh if-double02.sh Please input first number:4please input second number:54 < ; 5 old boy script 1:[[email protected] day4]# cat if-multi01.sh #!/bin/shif [$1-lt $];then echo "$ < $" Elif [$1-eq $];then echo "$ = $" Else echo "$ > $" fi resolve command-line arguments, no arguments, error questions: [[email protected] day4]# cat Judge-if-multi0 1-argv.sh #!/bin/shif [$#-ne 2];then echo "$ Usage method:num1 num2" Exit 1fiif [$1-lt $];then echo "$ < "Elif [$1-eQ $];then echo "$ = $" Else echo "$ > $" fi solves the problem of input non-integers: Filter out numbers: [[email protected] day4]# echo "3434dkjfkddf D99 "| Sed ' s#[a-z]# #g ' 343499 in turn, filter the numbers to see if the field is 0. Remove the number with a length of 0, indicating the number [[email protected] day4]# echo "3434dkjfkddfd99" | Sed ' s#[0-9]# #g ' dkjfkddfd[[email protected] day4]# cat judge-if-multi01-argv.sh #!/bin/shif [$#-ne 2];then Echo "$ Usage method:num1 num2" Exit 1fi[-N "' Echo $1|sed ' s#[0-9]# #g '"] && {echo "NUM1 must be int." exit}[-N "' Echo $2|sed ' s#[0-9]# #g '"] && {echo "num2 must be int." exit}if [$1-lt $];then echo "$ < $" Elif [$1-eq $];then echo "$ = $" Else echo "$ > $" fi test: [[EM Ail protected] day4]# sh judge-if-multi01-argv.sh nu3 34v 343judge-if-multi01-argv.sh Usage method:num1 num2[[ Email protected] day4]# sh judge-if-multi01-argv.sh nu3 34vnum1 must be int.[[email protected] day4]# sh judge- If-multi01-argv.sh 34vnum2 must be int.[[email protected] day4]#SH judge-if-multi01-argv.sh 343 > 3[[email protected] day4]# sh judge-if-multi01-argv.sh for 4343 = 43[[EMAIL&NB Sp;protected] day4]# sh judge-if-multi01-argv.sh 5343 < 53
4. Multiple methods for judging whether a string is a number 4.1 sed home plus expression
[ -n "`echo $num|sed ‘s/[0-9]//g‘`" ] && echo "第二个参数必须为数字" && exit 1条件表达式,大括号的用法:[ -n "`echo $num|sed ‘s/[0-9]//‘`" ] && { echo "第二个参数必须为数字" exit 1}
4.2 The substring of the variable replaces the home plus expression
[[email protected] day4]# num=521old521[[email protected] day4]# [ -z "`echo "${num//[0-9]/}"`" ] && echo 1 || echo 00[[email protected] day4]# 这个结果说明前面的结果不为1,字符串非空,即有非数字字符————————————————[[email protected] day4]# num=521[[email protected] day4]# [ -z "`echo "${num//[0-9]/}"`" ] && echo 1 || echo 01这个结果说明前面的结果去掉数字后为1,即没有非数字字符
4.3 The substring of the variable replaces the home plus expression (special judgment idea)
4.4 Expr calculation judgment
把变量和整数相加看是否成功执行:[[email protected] day4]# expr $1 + 0 > /dev/null 2>&1[[email protected] day4]# [ $? -eq 0 ] && echo int
5. Read-in method to determine the integer size
我的脚本:#!/bin/shread -p "please input first num:" num1read -p "please input second num:" num2[ -n "`echo $num1|sed ‘s#[0-9]##g‘`" ] && { echo "num1 must be int." exit}[ -n "`echo $num2|sed ‘s#[0-9]##g‘`" ] && { echo "num2 must be int." exit}if [ $num1 -lt $num2 ];then echo "$num1 < $num2"elif [ $$num1 -eq $$num2 ];then echo "$num1 = $num2"else echo "$num1 > $num2"fi老男孩的脚本1:[[email protected] day4]# vim read03.sh#!/bin/shread -p "please input tow num:" a bexpr $a + 0 > /dev/null 2>&1RETVAL1=$?expr $b + 0 > /dev/null 2>&1RETVAL2=$?[ "$RETVAL1" -eq 0 -a $RETVAL2 -eq 0 ] || { echo "argv1 and argv2 must be int." exit 2}if (( $a<$b ));then echo "$a<$b"elif [ $a -eq $b ];then echo "$a=$b"else echo "$a>$b"fi
Second, MySQL monitoring 1, method one 1.1 listening port
My script 1:[[email protected] mysql]# cat 01.sh #!/bin/bash# Pang Duan mysqld startingif ["' Netstat-tan | grep 3306 | awk ' {print $6} ' | Head-1 ' "= =" LISTEN "]; Then echo "Mysqld was starting" Else echo "mysqld is stop. Now is starting mysqld service ... "Systemctl start Mysqld.service pid=$ (lsof-i:3306 | head-2 | grep mysqld | awk ' {print $} ') echo "Staring mysqld ok,the mysqld PID is $pid" fi my script 2:[[email protected] mysql]# cat 02.sh #!/bin/ bash# Pang Duan mysqld startingif ["' Systemctl status Mysqld.service | grep Active | awk ' {print $} ' "= =" active "]; Then echo "Mysqld was starting" Else echo "mysqld is stop. Now is starting mysqld service ... "Systemctl start Mysqld.service pid=$ (lsof-i:3306 | head-2 | grep mysqld | awk ' {print $} ') echo "Staring mysqld ok,the mysqld PID is $pid" fi[[email protected] mysql]# old boy's script 1 (this method has a problem, the process is killed, fetch Value is null, error): [[email protected] mysql]# cat judgedb_by_port01.sh #!/bin/shport= ' netstat-lnt | grep 3306 | Awk-f ' [:]+ ' {print $} ' if [$port-ne 3306];then echo "MySQL is stoped" systemctl start mysqldfi old boy script 1 "improved version" (Fetch Netstat number of rows displayed): [[email protected] mysql]# cat judgedb_by_port01.sh #!/bin/shportnum= ' netstat-lnt | grep 3306 | Wc-l ' If [$portnum-ne 1];then echo "MySQL is stoped" systemctl start mysqldelse echo "MySQL is running" fi test: [[ Email protected] mysql]# sh judgedb_by_port01.sh MySQL is stoped[[email protected] MySQL]# sh judgedb_by_ port01.sh MySQL is running[[email protected] mysql]#
2, method two 2.0 monitoring ports and processes
My script 1:[[email protected] mysql]# cat judgedb_by_port01.sh #!/bin/shportnum= ' netstat-lnt | grep 3306 | Wc-l ' pid= ' ps-ed | grep mysqld | Wc-l ' If [$portnum-ne 1-o $pid-le 1];then echo "MySQL is stoped" systemctl start mysqldelse echo "MySQL is R Unning "fi[[email protected] mysql]# bash-x judgedb_by_port01.sh + wc-l++ netstat-lnt++ grep 3306+ portnum=0++ GRE P mysqld++ wc-l++ ps-ed+ pid=0+ ' [' 0-ne 1-a 0-le 1 '] ' + echo ' mysql is stoped ' MySQL is stoped+ systemctl start MySQL D old boy's script 1:[[email protected] mysql]# cat judgedb_by_port02.sh #!/bin/shportnum= ' netstat-lnt | grep 3306 | Wc-l ' processsum= ' ps-ef | grep MySQL | Grep-v grep | Wc-l ' If [[$portnum-eq 1 | | $processsum-eq 2]];then echo "MySQL is running" else echo "MySQL is stoped" echo " Staring MySQL ... "systemctl start MYSQLDFI test: [[email protected] mysql]# sh-x judgedb_by_port02.sh + wc-l++ Nets tat-lnt++ grep 3306+ portnum=0++ grep-v grep++ wc-l++ ps-ef++ grep mysql+ processsum=0+ [[0-eq 1]]+ [[0-eq 2]]+ Echo ' mysql is stoped ' MySQL is stoped+ systemctl start mysqld[[email protecte D] mysql]# [[email protected] mysql]# [[email protected] mysql]# sh-x judgedb_by_port02.sh + wc-l++ netstat- lnt++ grep 3306+ portnum=1++ grep mysql++ grep-v grep++ ps-ef++ wc-l+ processsum=2+ [[1-eq 1]]+ Echo ' MySQL is Runni Ng ' MySQL is running[[email protected] mysql]# old boy script 2:[[email protected] mysql]# cat judgedb_by_port03.sh #!/ Bin/bash#function:check MySQL statusmysqlstartup= "systemctl start Mysqld.service" dbprocesscount= ' Ps-ef | grep MySQL | Grep-v Brep | Wc-l ' dbportcount= ' netstat-lnt | grep 3306 | Wc-l ' If [$DbProcessCount-eq 2] && [$DbPortCount-eq 1];then echo "MySQL is running" Else $MySQLSTARTUP >/tmp/mysql.log sleep 10; Dbprocesscount= ' Ps-ef | grep MySQL | Grep-v grep | Wc-l ' dbportcount= ' netstat-lnt | grep 3306 | Wc-l ' If [$DbProcessCount-ne 2] | | [$DbPortCount-ne 1];then PKIll mysqld >/dev/null 2>&1 sleep 5 pkill mysqld >/dev/null 2>&1 sleep 5 [$DbProcessCoun T-eq 0] && mysqlstartup >>/tmp/mysql.log [$?-eq 0] && echo "MySQL is started" fi mail -S "MySQL restarted" [email protected] </TMP/MYSQL.LOGFI
2.2 Conversion Script format for UNIX mode
[[email protected] MySQL]# dos2unix judgedb_by_port03.sh dos2unix: converting file judgedb_by_port03.sh to Unix format ...
3. Method Three 3.0 Script test connection
My script 1:[[email protected] mysql]# cat 03.sh #!/bin/shvalue= ' mysql-uroot-p ' root '-s/var/lib/mysql/mysql.sock-e ' Select VERSION (); ">/dev/null 2>&1;echo $? ' portnum= ' netstat-lnt | grep 3306 | Wc-l ' processsum= ' ps-ef | grep MySQL | Grep-v grep | Wc-l ' If [[$portnum-eq 1 && $processsum-eq 2 && value-eq 0]];then echo "MySQL is running" Else E Cho "MySQL is stoped" echo "Staring MySQL ..." systemctl start mysqld version= ' mysql-uroot-p ' root '-s/var/lib/m Ysql/mysql.sock-e "Select VERSION ();" | Grep-v "()" ' Echo ' the MySQL version is $Version "fi old boy script 1:[[email protected] mysql]# CP judgedb_by_port02.sh Jud Gedb_by_mysql04.sh[[email protected] mysql]# cat judgedb_by_mysql04.sh#!/bin/shmysql-uroot-proot-s/var/lib/ Mysql/mysql.sock-e "Select VERSION ();" >/dev/null 2>&1if [[$?-eq 0]];then echo "MySQL is running" else echo "MySQL is stoped" echo "Staring MySQL ..." systemctl start Mysqldfi offsite connection, using-H IP
4, method four 4.1 through the Php/java program monitoring MySQL three, monitoring nginx or APACHE1, 1.1 to see if Nginx operation
[[email protected] ~]# Netstat-tan | grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp6 0 0::: 80 :::* LISTEN [[email protected] ~]# lsof-i: 80[[email protected] ~]# Ps-ef | grep nginx[[email protected] ~]# curl-i 10.201.106.134[[email protected] ~]# wget 10.201.106.134[[email protected] ~]# Curl-o/dev/null-s-w "%{http_code}" 10.201.106.134% total% Received% xferd Average speed Time Time Dload Upload Total spent left Speed100 3700 100 3700 0 0 695k 0--:--:----:--:----:--:--0curl-s silent Display [[email protected] ~]# nmap 10.201.106.1 34-p 80 | grep Open | Wc-l1[[email protected] ~]# Curl-o/dev/null-s-W%{http_code} http://10.201.106.134200[[email protected] ~ ]# [[email protected] ~]# curl-s-I http://10.201.106.134 | Sed-n ' 1p ' | Cut-d ""-F2200
1.2 Namp to determine if Nginx started
[[email protected] nginx]# vim judgeweb_by_port01.sh #!/bin/shportnum=`nmap 10.201.106.134 -p 80 | grep open | wc -l`if [ $portnum -ne 1 ];then echo "nginx is stoped" systemctl start nginxelse echo "nginx is running"fi
1.3 wget
[[email protected] nginx]# vim judgeweb_by_wget-url02.sh #!/bin/shwget -T 15 -q --spider http://10.201.106.134if [ $? -ne 0 ];then echo "nginx is stoped" systemctl start nginxelse echo "nginx is running"fi
1.4 Get Status Code judgment
[[email protected] nginx]# vim judgeweb_by_curl-code03.sh #!/bin/shhttpcode=`curl -o /dev/null -s -w %{http_code} http://10.201.106.134`if [ "$httpcode" != "200" ];then echo "nginx is stoped" systemctl start nginx else
Shell Learning Notes (3)