欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux初级:if语句的基本用法举例

程序员文章站 2024-01-05 12:03:12
...

1.输入分数,60以下失败,60-80不错,80-100NB!

#!/bin/bash

#**************************************************
#Author:     Du_shan
#QQ:         316722220 
#Date:       2018-08-30        
#FileName:    score_60.sh
#URL:         https://blog.csdn.net/weixin_40001704
#Description: The script test
#Copyright(C):2018 all rights reserved
#**************************************************
read -p "please input your score:" score
if [[ "$score" =~ ^[[:digit:]]+$ ]] ;then
        true
else
        echo "please input a digit"
        exit 60
fi


if  [ "$score" -ge 0 -a "$score" -le 60 ];then
        echo "fail"
elif [ "$score" -gt 60 -a "$score" -le 80 ];then
        echo "GOOD!"
elif [ "$score" -gt 80 -a "$score" -le 100 ];then
        echo "NB"
else
        echo "wrong score" 
fi

 

2.询问是否有钱,有钱你就赢了;没钱继续询问是不是帅,帅的话,小白脸儿你赢了;不帅还每钱,那等什么骚年,努力工作吧!

#!/bin/bash
#
#**************************************************************
#Author:                 Dadda_Du
#QQ:                     316722220
#Date:                   2018-08-31
#FileName:               ask_60.sh
#URL:                     https://blog.csdn.net/weixin_40001704
#Description:             The script test
#Copyright(C):            2018 all rights reserved
#**************************************************************
read -p "Are you rich ? (Yes or No):  " answer
if [[ "$answer" =~ ^[Yy]([Ee][Ss])?$ ]];then
        echo OK! You are win!
elif [[ "$answer" =~ ^[Nn][Oo]?$ ]];then
        read -p "Are you handsome ? (Yes or No): " answer
        if [[ "$answer" =~ ^[Yy]([Ee][Ss])?$ ]];then
                echo OK! Toy boy~U are win!
                exit
        elif [[ "$answer" =~ ^[Nn][Oo]?$ ]];then
                echo Work Hard! SaoNian!
        else
                echo "Please input Yes or No"
        fi
else
        echo Please input Yes or No!
fi

 

相关标签: if

上一篇:

下一篇: