shell double branch if statement

a grammar
if [conditional judgment]
then
The program to be executed when the condition is met
else
If the condition is not met, the program to be executed
be
 
Second, determine whether the input is a directory
#!/bin/bash
rate=$(df -h|grep "/dev/sda3"|awk '{print $5}'|cut -d "%" -f1)
if [ $rate -le 80 ]
then
echo "/dev/sda3 is not full"
be
 
Three running results
please input a dir:/root
is dir
[root@localhost shell]# ./shell3.sh
please input a dir:fd^H^H
no no no
 
Four to determine whether Apache is running
#!/bin/bash
test = $ (ps aux | grep httpd | grep -v grep)
if [ -n "$test" ]
then
echo "$(date) httpd is OK"
else
service httpd start &>/dev/null
echo "$(date) restart httpd!! "
be
 
Five running results
[root@localhost shell]# ./shell4.sh
Fri Jul 28 21:18:32 CST 2017 httpd is OK
[root@localhost shell]# service httpd stop
Redirecting to /bin/systemctl stop httpd.service
[root@localhost shell]# ./shell4.sh
Fri Jul 28 21:19:04 CST 2017 restart httpd!!
 
Six precautions
The file name of shell4.sh cannot contain httpd, otherwise the operation will fail.
Because when ps aux|grep httpd is executed, the keyword httpd in the file name will be included.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326173310&siteId=291194637