Learning with a master LINUX notes -20

Section 20: conditional statement and if the use of
this section is to talk:
20.1 read keyboard commands to read the values of variables
read command is the most important input parameter read
important parameters
-p: prompt followed by the contents
read -p "Please enter your name: "$ name
-t: followed by a number, this number exceeds the number of seconds is not entered automatically exit
read -t 5 -p" Please enter your name: "$ name
5 seconds without input automatically out
when the input content is not displayed, the password used to: -s
Example. 1:
[@ the root Node-scripts. 1] test-read.sh CAT #
# / bin / the bash!
Read -p "Please iNPUT your name:" nAME
Read -p "Please input your age:" AGE

cat << eof


Your basic information is as follows:
Name: $ NAME
Age: $ AGE


eof

20.2 statement if flow control
flow control statement if Syntax:
if [Condition judging]
the then
command
elif
command
else
command
fi
Description of significance if introduce Comparative if before
comparing the value of 2.1
-eq: equality
-nq: are not equal
- gt: is greater than
-lt: is less than
-ge: not less than a
-le: whether or less
Example 2:
[@ the root Node-scripts. 1] test2.sh CAT #
# / bin / the bash!
Read -p "INPUT num1 num2 : "num1 num2
IF [-gt $ $ num1 num2]
the then
echo" $ num1> num2 $ "
elif [-LT-$ $ num1 num2]
the then
echo" num1 $ <$ num2 "
the else
echo" $ num1 num2 $ = "
Fi
2.2 commonly used parameter string
==: equal to the true
=:! true not equal to
the -Z: true length of the string is zero
Example 3:
[root @ the Node-1 scripts] # CAT test3.sh
! # / bin / bash
the Read -p "the INPUT your name:" name
IF [$ name == "root"]
the then
echo "you are Super Administrator"
the else
echo " general User A are by You "
Fi
2.3 document is determined common parameters
-e: whether the file exists
-f: if a regular file
-d: whether the folder
-r: is readable
-w: is writable
-x: is executable
Example 4: empty the log file to retain the first 100 lines
[root @ the Node-1 scripts] # CAT clear-log.sh
! # / bin / bash
# whether the root user
[! $ the uSER = "root"] iF
the then
echo "you the root BE MUST! "
Exit 0
Fi
# determines whether a file exists
iF [! -f / var / log / messages]
the then
echo" file Not eXISTS! "
echo 1
fi
tail -100 /var/log/messages > /var/log/mesg.tmp
rm -rf /var/log/messages
cat /var/log/mesg.tmp > /var/log/messages

Example 5
requirements are as follows:
Creating a named /root/scripts/check.sh script on the server, the following functions:
1) When running /root/check.sh redhat, output CentOS
2) When run / root / check. sh centos, redhat output
. 3) when no parameters or parameter is not redhat or centos, whose output produces the following error message:
/root/scripts/check.sh redhat | CentOS
[@ the root Node-scripts. 1] Check CAT # .sh
#! / bin / bash
IF [ "$ 1" == "RedHat"]
the then
echo "CentOS"
elif [ "$ 1" == "CentOS"]
the then
echo "RedHat"
the else
echo "/ root / scripts / the Check. RedHat SH | CentOS "
fi
run test results
[root @ the Node-1 scripts] # SH check.sh RedHat
CentOS
[root @ the Node-1 scripts] # SH check.sh CentOS
RedHat
[root @ the Node-1 scripts] # SH the Check .sh aa
RedHat /root/scripts/check.sh | CentOS
20.4 flow control process complex conditions and wildcards
complex conditions:
-a && conditions and with
-o and conditions or ||
*: matches zero or more characters
: matching a character?

例6:
[root@node-1 scripts]# cat /etc/profile |less

IF [-gt the UID $ 199] && [ " /usr/bin/id -gn" = " /usr/bin/id -un"]; the then
the umask 002
the else
the umask 022
Fi
20.5 combat - shell script several daily use actual
combat scenario 1:
Analyzing the host CPU utilization
[root @ node-1 scripts] # cat checkcpu.sh

# / bin / the bash
#environment variable
Source / etc / Profile
#cpu usage
cpu_us = vmstat | awk '{print $13}' | sed -n '$p'
cpu_sy = vmstat | awk '{print $14}' | sed -n '$p'
the cpu_id = vmstat | awk '{print $15}' | sed -n '$p'
cpu_sum = $ (($ + $ cpu_us cpu_sy))
IF ((CPU $ SUM> = 90))
the then
MSG = "the TIME : $ (DATE% F. +
% T)
hOSTNAME: $ (hostname)
the IPADDR: $ (the ifconfig | awk 'NR == $ 2} 2 {Print')
the MSG: the CPU usage is too high already spent $ {cpu_sum}%! "
echo $ msg
fi
execution result
[root @ the Node-1 scripts] # SH checkcpu.sh
the TIME: 2020-04-02_23: 23: 17 HOSTNAME: the Node-1 IPADDR: 192.168.26.71 the mSG: the CPU usage is too high! I have used 92%

Actual script 2:
determine whether the installation Failure to install the vsftpd Installation - Other services put their names changed a bit
[root @ the Node-1 scripts] # CAT chkvsftpd.sh
! # / Bin / bash
RPM -qa | grep vsftpd &> / dev / null
IF [$? -ne 0]
the then
yum -y install vsftpd &> / dev / null && echo "vsftpd Installed suecess."
IF [$? -eq 0]
the then
systemctl restart vsftpd vsftpd && systemctl enable
the else
echo "vsftpd failure install! "
fi
fi

The results:
[root @ the Node-1 scripts] # SH chkvsftpd.sh
. "Vsftpd Installed suecess"
the Created from /etc/systemd/system/multi-user.target.wants/vsftpd.service symlink to / usr / lib / systemd /system/vsftpd.service.

Actual script 3:
test whether a specified IP address can ping
[root @ the Node-1 scripts] # CAT chkping.sh
! # / Usr / bin / bash
the Read -p "Please the INPUT A ip:" ip
ping -c $ 3 IP &} {> / dev / null
IF [$? -eq 0]
the then
echo "$ {IP} IS up now."
the else
echo "$ {IP} IS Down."
Fi

4 combat script: determining whether the port 80 is turned on
[@ the root Node-scripts. 1] chk80port.sh CAT #
# / bin / the bash!
Port = netstat -lnp | grep 80
IF [the -Z "Port"]; the then
echo "Not Start-Service.";
Exit;
fi
web_server = echo $port | awk -F'/' '{print $2}'|awk -F : '{print $1}'
Case $ web_server in
httpd)
echo "the Apache Server."
;;
nginx)
echo "Server nginx."
;;

  • )
    echo "other server."
    ;;
    esac

Test results:
[root @ the Node-1 scripts] # SH chk80port.sh
the Apache Server.

Actual script 5:
compile and install the httpd service control script
Description: httpd source installation location in / usr / local / apache directory
! # / Bin / bash
IF [$ 1 == Start]; the then
netstat -utpln | grep 80 &> / dev / null
IF [$ -eq 0?]; the then
"! httpd IS Started" echo
Exit 1
the else
/ usr / local / the Apache / bin / apachectl Start &> / dev / null
echo "+ / usr / local / the Apache / bin / apachectl Start "
echo" + -utpln the netstat | grep 80 "
the netstat -utpln | grep 80
the PID = $ (-utpln the netstat | grep 80 | awk '{}. 7 Print $' | awk -F '/' '. 1 $ {Print } ')
echo -e "the PID IS running IS the httpd the PID $!"
Fi
elif [STOP == $. 1]; the then
the netstat -utpln | grep 80 &> / dev / null
IF [$ 0 -ne?]; the then
echo "httpd is stoped!"
exit 1
else
/usr/local/apache/bin/apachectl stop &>/dev/null
echo "+/usr/local/apache/bin/apachectl stop"
sleep 2
echo "+netstat -utpln |grep 80"
netstat -utpln |grep 80
if [ $? -ne 0 ];then
echo "httpd is stopping!"
fi
fi
elif [ $1 == restart ];then
netstat -utpln |grep 80 &>/dev/null
if [ $? -ne 0 ];then
echo "httpd not is started!"
/usr/local/apache/bin/apachectl start &>/dev/null
echo "+/usr/local/apache/bin/apachectl start"
sleep 2
echo "+netstat -utpln |grep 80"
netstat -utpln |grep 80
if [ $? -eq 0 ];then
echo "httpd is started!"else
fi

/usr/local/apache/bin/apachectl stop &>/dev/null
echo "+/usr/local/apache/bin/apachectl stop"
sleep 2
/usr/local/apache/bin/apachectl start &>/dev/null
echo "+/usr/local/apache/bin/apachectl start"
sleep 2
echo "+netstat -utpln |grep 80"
netstat -utpln |grep 80
if [ $? -eq 0 ];then
echo "httpd is started!"
fi
fi
elif [ $1 == status ];then
netstat -utpln |grep 80 &>/dev/null
if [ $? -eq 0 ];then
echo "httpd is running!"
PID=$(netstat -utpln |grep 80 |awk '{print $7}'|awk -F'/' '{print $1}')
echo -e "httpd is running ! PID is $PID"
else
echo "httpd is not running!"
fi
else
echo "Usage: /etc/init.d/httpd start|stop|restart|status "
fi

Guess you like

Origin blog.51cto.com/450955/2484533
Recommended