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
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律