zoukankan      html  css  js  c++  java
  • linux shell if语句

    1、测试1

    [root@centos7 test2]# ls
    a.txt
    [root@centos7 test2]# if [ -e a.txt ]; then echo "exist";else echo "no nxist"; fi
    exist
    [root@centos7 test2]# if [ -e b.txt ]; then echo "exist";else echo "no nxist"; fi
    no nxist

    2、测试2

    [root@centos7 test2]# seq 3 > a.txt
    [root@centos7 test2]# seq 5 > b.txt
    [root@centos7 test2]# anum=$(wc -l a.txt|awk '{print $1}')
    [root@centos7 test2]# bnum=$(wc -l b.txt|awk '{print $1}')
    [root@centos7 test2]# if [ $anum -gt $bnum ]; then echo "a > b"; else echo "b > a"; fi
    b > a

    3、测试3

    [root@centos7 test2]# cat test.sh
    #!/bin/bash
    read -p "please input your age: " age
    if [[ $age =~ [^0-9] ]]; then
    echo "please input an integer."
    exit 10
    elif [ $age -gt 150 ]; then
    echo "your age is wrong."
    exit 20
    elif [ $age -gt 20 ]; then
    echo "good good work, day day up"
    else
    echo "good good study, day day up"
    fi
    [root@centos7 test2]# bash test.sh
    please input your age: abcde
    please input an integer.
    [root@centos7 test2]# bash test.sh
    please input your age: 200
    your age is wrong.
    [root@centos7 test2]# bash test.sh
    please input your age: 35
    good good work, day day up
    [root@centos7 test2]# bash test.sh
    please input your age: 14
    good good study, day day up

    4、

    [root@centos7 test2]# cat test.sh
    #!/bin/bash
    read -p "please input your score: " score
    if [[ $score =~ [^0-9] ]]; then
    echo "please input an integer."
    exit 10
    elif [ $score -gt 100 ]; then
    echo "your score is wrong."
    exit 20
    elif [ $score -gt 85 ]; then
    echo "your score is very good."
    elif [ $score -ge 60 ]; then
    echo "your score is soso."
    else
    echo "you are loser."
    fi
    [root@centos7 test2]# bash test.sh
    please input your score: abcde
    please input an integer.
    [root@centos7 test2]# bash test.sh
    please input your score: 200
    your score is wrong.
    [root@centos7 test2]# bash test.sh
    please input your score: -13
    please input an integer.
    [root@centos7 test2]# bash test.sh
    please input your score: 99
    your score is very good.
    [root@centos7 test2]# bash test.sh
    please input your score: 78
    your score is soso.
    [root@centos7 test2]# bash test.sh
    please input your score: 34
    you are loser.

    参考:https://mp.weixin.qq.com/s/rTsLIb2p-AE3oDPQXMQmLw

  • 相关阅读:
    sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)
    poj 2406:Power Strings(KMP算法,next[]数组的理解)
    hrbustoj 1551:基础数据结构——字符串2 病毒II(字符串匹配,BM算法练习)
    hrbustoj 1179:下山(DFS+剪枝)
    [Linux] 通过指令修改时区 tzselect
    Ubuntu 历史版本下载
    RK3288 USB UVC camera 摄像头 VIDIOC_DQBUF Failed!!! err[I/O error]
    Android Activity活动状态及生存周期
    Android 系统四大组件
    Android JNI访问Java成员
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14689068.html
Copyright © 2011-2022 走看看