Shell基础之条件判断 & 分支判断

1,030 阅读2分钟

条件判断式语句

文件类型判断

测试选项 作用
-d 文件 是否是目录
-e 文件 是否存在
-f 文件 是否是文件

整数之间的比较

测试选项 作用
整数1 -eq 整数2 相等
整数1 -ne 整数2 不相等
整数1 -gt 整数2 大于
整数1 -lt 整数2 小于
整数1 -ge 整数2 大于等于
整数1 -le 整数2 小于等于

文件权限

测试选项 作用
-r 文件 是否可读
-w 文件 是否可写
-x 文件 是否可执行

字符串判断

测试选项 作用
-z 字符串 是否为空,为空返回真
-n 字符串 是否为空,非空返回真
字符串1 == 字符串2 ==
字符串1 != 字符串2 !=

多重条件判断

测试选项 作用
判断1 -a 判断2
判断1 -o 判断2
!判断
# 两种判断格式
# test -e /root/install.log
# [-e /root/install.log] //推荐

[-d /root] && echo "yes" || echo "no"

单分支if语句

#!/bin/bash

# 判断登陆的用户是否是root
test=$(env | grep "USER" | cut -d "=" -f 2)

if [ "$test" == root ]
        then
                echo "Current user is root"
fi
#!/bin/bash

# 判断分区使用率
rate=$(df -h | grep "/dev/vda1" | awk '{print $5}' | cut -d "%" -f 1)

if [ $rate -ge 1 ]
        then
                echo "Warming /dev/vda1 is full !!"
fi

双分支if语句

#!/bin/bash

read -t 30 -p "Please input a dir: " dir

if [ -d "$dir" ]
	then
		echo "yes"
	else
		echo "no"
fi
#!/bin/bash

#检测apache的运行状态(脚本名字坚决不能包含httpd,否则脚本会失效)
test=$(ps aux | grep httpd | grep -v grep)

if [ -n "$test" ]
    then
        echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log
    else
        /etc/rc.d/init.d/httpd start &> /dev/null
        echo "$(date) httpd restart httpd!!" >> /tmp/autostart-acc.log
fi

多分支if语句

#!/bin/bash

read -p "Please input a filename : " file

if [ -z "$file" ]
	then
		echo "Error, please input a filename "
		exit 1
elif [ ! -e "$file" ]
	then
		echo "Your input is not a file! "
		exit 2
elif [ -f "$file" ]
	then
		echo "$file is a regulare file!"
elif [ -d "$file" ]
	then 
		echo "$file is a directory!"
else
		echo "$file is an other file!"
fi

多分支case语句

#!/bin/bash

read -t 30 -p "Please input yes/no : " cho

case "$cho" in
	"yes")
		echo "yes"
		;;
	"no")
		echo "no"
		;;
	"*")
		echo "Please input right content"
		;;
esac

for循环

#!/bin/bash

# 批量解压文件
cd /root/test/
ls *.tar.gz > ls.log
ls *.tgz >> ls.log

for i in $( cat ls.log )
    do
        tar -zxf $i & > /dev/null
    done
    
rm -rf ls.log
#!/bin/bash

s=0
for (( i=1;i<=100;i=i+1 ))
    do
        s=$(( $s+$i ))
    done
echo $s
#!/bin/bash

# 批量添加指定数量的用户

read -p "Please input user name : " -t 30 name
read -p "Please input number of users : " -t 30 num
read -p "Please input user pass : " -t 30 pass

if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]
    then
    y=$(echo $num | sed 's/[0-9]//g')
    if [ -z "$y" ]
        then
        for (( i=1;i<=$num;i=i+1 ))
            do
                /usr/sbin/useradd $name$i &> /dev/null
                echo $pass | /usr/bin/passwd --stdin $name$i &> /dev/null
            done
    fi
fi
#!/bin/bash

#批量删除

for i in $( cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1 )
    do
        userdel -r $i
    done

while循环和until循环

#!/bin/bash

i=1
s=0

while [ $i -le 100 ]
    do
        s=$(( $s+$i ))
        i=$(( $i+1))
    done

echo "This sum is : $s"
#!/bin/bash

i=1
s=0

util [ $i -ge 100 ]
    do
        s=$(( $s+$i ))
        i=$(( $i+1))
    done

echo "This sum is : $s"