Shell条件判断

一、文件类型判断

示例:

# 判断文件是否存在,存在为0, 不存在为1
[root@localhost ~]# test -e person.txt
[root@localhost ~]# echo $?
0
[root@localhost ~]#
[root@localhost ~]# test -e aba
[root@localhost ~]# echo $?
1
# 出test外,可以使用中括号
[root@localhost ~]# [ -e person.txt ]
[root@localhost ~]# echo $?
0
[root@localhost ~]#
[root@localhost ~]# [ -d /home ] && echo "is directory" || "is not directory"
is directory
[root@localhost ~]# [ -f ./person.txt ] && echo "yes" || echo "no"
yes

1.2 文件权限判断

[root@localhost ~]# [ -r ./person.txt ] && echo "yes" || echo "no"
yes

1.3 两个文件的判断

[root@localhost ~]# touch abc.txt
[root@localhost ~]# [ person.txt -nt abc.txt ] && echo "yes" || echo "no"
no

1.4 整数比较

[root@localhost ~]# [ 1 -gt 2 ] && echo "yes" || echo "no"
no

1.5 字符串判断

示例:

[root@localhost ~]# name=zs
[root@localhost ~]# [ -z "$name" ] && echo "yes" || echo "no"
no
[root@localhost ~]# [ "abc" == "bcd" ] && echo "yes" || echo "no"
no
[root@localhost ~]# [ "abc" == "abc" ] && echo "yes" || echo "no"
yes
[root@localhost ~]#

1.6 多重判断

示例:

[root@localhost ~]# num=100
# 判断num是否存在,且大于200
[root@localhost ~]# [ -n "$num" -a "$num" -gt 200 ] && echo "yes" || echo "no"
no
[root@localhost ~]# num=201
# 判断num是否存在,且大于200
[root@localhost ~]# [ -n "$num" -a "$num" -gt 200 ] && echo "yes" || echo "no"
yes
[root@localhost ~]#

二、流程控制

2.1 if分支

if [ condition ]; then
语句
fi
或者
if [ condition ]
then
语句
fi
示例 1
[root@localhost ~]# vim iftest.sh
#!/bin/bash
#author:test
num=$1
if [ $num -ge 100 ];then
echo ">100"
fi
[root@localhost ~]# vim iftest.sh
#!/bin/bash
#author:test
num=$1
if [ $num -ge 100 ];then
echo ">100"
fi
[root@localhost ~]# chmod 777 iftest.sh
[root@localhost ~]# ./iftest.sh 102
>100

示例2:

#!/bin/bash
# author: tt
# description: 当sda1盘的占用到90%即输出警告信息
rate=$(df -h | grep "/dev/sda1" | awk '{print $5}' | cut -d "%" -f 1)
if [ $rate -ge 90 ]
then
echo "Warning ! /dev/sda1 is full"
fi
if else 双分支结构
if [ condition ]
then
语句
elif [ condition ]; then
语句
else
语句
fi
示例 1
[root@localhost ~]# vim iftest02.sh

#!/bin/bash
#author:tt

num=$1
if [ $num -gt 100 ]
then
echo "num > 100"
else
echo "num <= 100"
fi

[root@localhost ~]# chmod 755 iftest02.sh
[root@localhost ~]# ./iftest02.sh 200
num > 100

示例2:

[root@localhost ~]# vim iftest03.sh
#!/bin/bash
read -p "please input a num:" num
if [ $num -gt 10 -a $num -le 100 ]
then
echo "100>=num>10"
exit 1
elif [ $num -gt 100 -a $num -le 1000 ]; then
echo "1000>=num>100"
exit 2
elif [ $num -gt 1000 -a $num -le 10000 ]; then
echo "10000>=num>1000"
exit 3
else
echo "other num"
fi
[root@localhost ~]# chmod 755 iftest03.sh
[root@localhost ~]# ./iftest03.sh
please input a num:1000
1000>=num>100
[root@localhost ~]#

2.2 case分支

if elif else 语句一样都是属于分支语句,不同点在于, case 只能判断一种条件关系, if 可以判断多种条
件关系。
case 语法:
case $conditionVar in
"val1" # 注意此处只能有一个条件,不能像 if 那样使用复杂的多条件
若值为 val1 ,则执行这里
;;
"val2")
若值为 val1 ,则执行这里
;;
...
*)
其他变量值则执行这里
;;
esac
示例:
[root@localhost ~]# vim casetest.sh
#!/bin/bash
# author: tt
# discription: 演示case语法
read -p "please input y/n " -t 30 inchoose
case $inchoose in
"y")
echo "your input is y"
;;
"n")
echo "your input is n"
;;
*)
echo "your input is others "
;;
esac
[root@localhost ~]# chmod 755 casetest.sh
[root@localhost ~]# ./casetest.sh
please input y/n y
your input is y

2.3 for循环

1 ) 用法一
for 变量 in 1 2 3 4 ...
do
执行语句
done
示例 1
[root@localhost ~]# vim fortest.sh
#!/bin/bash
# author: tt
# description: for demo
for i in 1 2 3 4 5 6
do
echo $i
done
[root@localhost ~]# chmod 755 fortest.sh
[root@localhost ~]# ./fortest.sh
...

示例2:

[root@localhost ~]# vim fortest2.sh
#!/bin/bash
for ((i=1;i<=100;i++));
do
echo $((i));
done

示例3:

[root@localhost ~]# vim fortest3.sh
#!/bin/bash
for i in $(seq 1 50)
do
echo $(expr $i)
done

示例4:

[root@localhost ~]# vim fortest4.sh
#/bin/bash
for i in {1..50}
do
echo $((i));
done

示例5:

[root@localhost ~]# vim fortest5.sh
#/bin/bash
list="hello shell word";
for str in $list
do
echo $str
done

示例6:

[root@localhost ~]# vim fortest6.sh
#/bin/bash
for f in /tmp/*;
do
echo $f;
done

2.4 while循环

只要满足循环条件则进行循环
格式:
while [ 条件判断 ]
do
语句
done
示例:
[root@localhost ~]# vim whiletest1.sh
#!/bin/bash
i=1
s=0
while [ $i -le 100 ]
do
s=$((s+i));
i=$((i+1));
done
echo "sum = $s"
until 循环
只有条件满足才退出循环
格式:
until [ 条件 ]
do
语句
done
示例:
[root@localhost ~]# vim untiltest.sh
#/bin/bash
i=0
s=0
until [ $i -gt 100 ]
do
s=$((s+i));
i=$((i+1));
done
echo "sum = $s"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值