判断结构--Linux

if判断结构

if expression; then
    command1
    command2
fi
majun@instance-zqtg07w6:~$ vim score01.sh
majun@instance-zqtg07w6:~$ cat score01.sh
#!/bin/bash
echo -n "Please input your score:"
read SCORE
if [ "$SCORE" -lt 60 ]; then
        echo "C"
fi
if [ "$SCORE" -lt 80 -a "$SCORE" -ge 60 ]; then
        echo "B"
fi
if [ "$SCORE" -ge 80 ]; then
        echo "A"
fi

majun@instance-zqtg07w6:~$ bash score01.sh
Please input your score:99
A
majun@instance-zqtg07w6:~$ bash score01.sh
Please input your score:78
B
majun@instance-zqtg07w6:~$ bash score01.sh
Please input your score:59
C
majun@instance-zqtg07w6:~$

if/else 判断结构

if expression; then
    command
else
    command
fi
majun@instance-zqtg07w6:~$ vim check_file.sh
majun@instance-zqtg07w6:~$ cat check_file.sh
#!/bin/bash
echo -n "input your file:"
read FILE
#FILE=/etc/passwd
if [ -e $FILE ]; then
        echo "$FILE exists."
else
        echo "$FILE NOT EXISTS."
fi
majun@instance-zqtg07w6:~$ bash check_file.sh
input your file:/etc/passwd
/etc/passwd exists.
majun@instance-zqtg07w6:~$ bash check_file.sh
input your file:/etc/passwd01
/etc/passwd01 NOT EXISTS.
majun@instance-zqtg07w6:~$

if/elif/else 判断结构

if expression; then
    command1
elif expression; then
    command2
elif expression; then
    command3
else
    command4
fi
majun@instance-zqtg07w6:~$ cat score03.sh
#!/bin/bash
echo -n "Please input your score:"
read SCORE
if [ "$SCORE" -lt 60 ]; then
        echo "CCCCCCCC"
elif [ "$SCORE" -ge 60 -a "$SCORE" -lt 80 ]; then
        echo "BBBBBBBB"
else
        echo "AAAAAAAA"
fi
majun@instance-zqtg07w6:~$ bash score03.sh
Please input your score:99
AAAAAAAA
majun@instance-zqtg07w6:~$ bash score03.sh
Please input your score:80
AAAAAAAA
majun@instance-zqtg07w6:~$ bash score03.sh
Please input your score:59
CCCCCCCC
majun@instance-zqtg07w6:~$ bash score03.sh
Please input your score:78
BBBBBBBB
majun@instance-zqtg07w6:~$

case 判断结构

majun@instance-zqtg07w6:~$ vim os_type.sh
majun@instance-zqtg07w6:~$ cat os_type.sh
#!/bin/bash
OS=`uname -s`
case "$OS" in
Linux) echo "this is linux;";;
SunOS) echo "this is sun";;
*) echo "failed to type os";;
esac
echo $OS

majun@instance-zqtg07w6:~$ bash os_type.sh
this is linux;
Linux
majun@instance-zqtg07w6:~$

所有的练习脚本都在:
https://github.com/SaltNego/Learn_linux_bash

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值