Shell script-if flow control statement (including application cases)

Basic grammatical structure

if structure

if [ condition ];then
        command
        command
fi

if test 条件;then
    命令
fi

if [[ 条件 ]];then
    命令
fi

[ 条件 ] && command

Instance

#!/bin/bash
	read a
	read b
	if (( $a == $b ))
	then
		echo "a和b相等"
	fi

if...else structure

if [ condition ];then
        command1
    else
        command2
fi

[ 条件 ] && command1 || command2

Instance

#!/bin/bash
	read a
	read b
	if (( $a == $b ))
	then
		echo "a和b相等"
	else
		echo "a和b不相等,输入错误"
	fi

#!/bin/env bash
   read -p "请输入一个字符串:" str
   if [ "$str" = "hello" ]
   then
       echo 'world'
   else
       echo "请输入hello!"
   fi

# 还可以这样写
read -p '请输入一个字符串:' str;
 [ "$str" = 'hello' ] && echo 'world' ||  echo '请输入hello!'

if...elif...else structure

if [ condition1 ];then
        command1      结束
    elif [ condition2 ];then
        command2       结束
    else
        command3
fi

如果条件1满足,执行命令1后结束;如果条件1不满足,再看条件2,如果条件2满足执行命令2后结束;
如果条件1和条件2都不满足执行命令3结束.

Instance

#!/bin/bash
printf "请输入数字: "
read num 
if ((num==1)); then
    echo "Monday"
elif ((num==2)); then
    echo "Tuesday"
elif ((num==3)); then
    echo "Wednesday"
elif ((num==4)); then
    echo "Thursday"
elif ((num==5)); then
    echo "Friday"
elif ((num==6)); then
    echo "Saturday"
elif ((num==7)); then
    echo "Sunday"
else
    echo "错误"
fi

Layers of nesting structure

if [ condition1 ];then
        command1        
        if [ condition2 ];then
            command2
        fi
 else
        if [ condition3 ];then
            command3
        elif [ condition4 ];then
            command4
        else
            command5
        fi
fi
如果条件1满足,执行命令1;如果条件2也满足执行命令2,如果不满足就只执行命令1结束;
如果条件1不满足,不看条件2;直接看条件3,如果条件3满足执行命令3;如果不满足则看条件4,如果条件4满足执行命令4;否则执行命令5

Insert picture description here

Instance

#!/bin/bash
while true
do
read -p "请输入你的成绩(满分100):" n
if [ -z $n ];then
        echo "请输入成绩(不能为空!)"
elif [[ $n =~ ^[^0-9]+$ ]];then
        echo "请输入成绩(只能是整数数字)"
else
        if [ $n -le 100 -a $n -ge 0 ];then
                echo "你考了$n分"
                break
        else
                echo "只能输入0-100的分数。"
        fi  
fi

done

if [ $n = 100 ];then
        echo "满分"
elif [ $n -ge 85 -a $n -lt 100 ];then
        echo "优秀"
elif [ $n -ge 60 -a $n -lt 85 ];then
        echo "良"
elif [ $n -ge 45 -a $n -lt 60 ];then
        echo "差"
elif [ $n -gt 0 -a $n -lt 45 ];then
        echo "没救了"
elif [ $n = 0 ];then
        echo "笨蛋"
fi

Applications

Determine whether the two hosts can be pinged

#!/bin/bash

# 交互式定义变量,让用户自己决定ping哪个主机
read -p "请输入你要ping的主机的IP:" ip

# 使用ping程序判断主机是否互通
ping -c1 $ip &>/dev/null

if [ $? -eq 0 ];then
    echo "当前主机和远程主机$ip是互通的"
 else
     echo "当前主机和远程主机$ip不通的"
fi

逻辑运算符
test $? -eq 0 &&  echo "当前主机和远程主机$ip是互通的" || echo "当前主机和远程主机$ip不通的"

Determine whether a process exists

#!/bin/bash
# 判断一个程序(httpd)的进程是否存在
read -p '请输入你想判断的进程:' process
pgrep $process &>/dev/null
if [ $? -ne 0 ];then
    echo "当前$process进程不存在"
else
    echo "当前$process进程存在"
fi

或者
test $? -eq 0 && echo "当前$process进程存在" || echo "当前$process进程不存在"

Determine whether the user exists

#!/bin/bash
#判断用户是否存在
read -p "请写出用户名" username
id $username
if [ $? -eq 0 ];then
        echo "用户存在"
else
        echo "用户不存在"
fi

#!/bin/bash
read -p '请输入用户名:' username
id $username &>/dev/null
[ $? -eq 0 ] && echo '用户存在' || echo '不存在'

Determine whether the package is installed

#!/bin/bash
read -p "请输入你想检测是否安装的软件名:" name
rpm -q $name &>/dev/null
if [ $? -ne 0 ];then
        yum install -y $name &>/dev/null
        if [ $? -eq 0 ];then
                echo "安装成功"
        else
                echo "安装失败"
        fi  
else
        echo "本机已经安装$name"
fi

Determine the kernel version of the current host

If the kernel version is different, the download method is different, Redhat is yum,
Debian is apt-get

#检查内核
#!/bin/bash
os_check() {
    
    
        if [ -e /etc/redhat-release ];then
                REDHAT=`cat /etc/redhat-release |cut -d ' ' -f1`
        else
                DEBIAN=`cat /etc/issue |cut -d ' ' -f1`
        fi  
    
        if [ "$REDHAT" == "CentOS" -o "$REDHAT" == "Red" ];then
                echo "Operating system is RedHat"
        elif [ "$DEBIAN" == "Ubuntu" -o "$DEBIAN" == "ubuntu" ];then
                echo "Operating system is Ubuntu"
        else
                echo "Operating system don't support"
                exit 1
        fi  
}

os_check


Judging Leap Year

Enter a year to determine whether it is a lucrative year (a year divisible by 4 but not 100, or a year divisible by 400 is a leap year)

#!/bin/bash
read -p "请输入一个年份:" year
if [ $[$year%4] -eq 0 -a $[$year%100] -ne 0 ];then
    echo "$year 是闰年"
elif [ $[$year%400] -eq 0 ];then
    echo "$year 是闰年"
else
    echo "$year 不是闰年"
fi

Guess you like

Origin blog.csdn.net/Cantevenl/article/details/115265555