当前位置:网站首页>【shell编程】第二章:条件测试语句
【shell编程】第二章:条件测试语句
2022-08-05 05:16:00 【六月的可乐】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
第二章:条件测试语句
前言
条件测试语句:
- 条件测试
- 数值运算
- if条件判断
一、知识点
(一)条件测试
1、语法


2、test条件测试语句
语法: test 测试参数 要测试的对象 对结果进行判断执行的动作
test -e hello.txt && echo “该文件存在” || echo “”该文件不存在
3、[]条件测试语句
注意:如果是测试变量必须要加上双引号
4、字符串比较测试:

5、数值比较测试:


(二)小结
写一个只能输入1、2的脚本
二、案例脚本
1.(())运算命令例子
#! /bin/bash
# 写一个计算器脚本
# 定义函数
print_tips() {
printf "必须输入纯数字!!!"
exit 1
}
# 让用户输入第一个数字
read -p "请输入第一个数字:" firstnumber
# read "请输入第一个数字:" firstnumber
# 判断用户输入的字符是否是存数字
if [ -n "`echo $firstnumber|sed 's/[0-9]//g'`" ]
then
print_tips
fi
# 让用户输入运算符 只能加减乘除
read -p "请输入运算符:" operate
if [ "$operate" != "+" ] && [ "$operate" != "-" ] && [ "$operate" != "*" ] && [ "$operate" != "/" ]
then
echo "只能输入+、-、*、%"
exit 2
fi
# 让用户输入第二个数字
read -p "请输入第二个数字:" sendnumber
# 验证第二个字符格式
if [ -n "`echo $sendnumber|sed 's/[0-9]//g'`" ]
then
print_tips
fi
# 输出运算结果:
echo "${firstnumber}${operate}${sendnumber}的结果是:$(($firstnumber$operate$sendnumber))"
2.let命令使用:一个检测网站存活的脚本
#!/bin/bash
# 定义变量
url=$1
echo "即将对$1进行检查>>>>>>>"
failCount=0
successCount=0
timeout=5
# 定义一个检测函数
check_url() {
while true
do
# --timeout表示超时时间s --tries表示反复重试 -q表示忽略详细信息 -O表示输出地点
wget --timeout=${timeout} --tries=1 $url -q -O /dev/null
if [ $? -ne 0 ]
then
let failCount=failCount+1
else
let successCount+=1
fi
# 根据统计次数给出相应的提示
if [ $failCount -ge 2 ]
then
printf "很遗憾!该网站存在问题,请及时检查:_:"
exit 1
elif [ ${successCount} -ge 1 ]
then
echo "恭喜!${url}网站运行正常,可以访问^ - ^"
exit 0
fi
done
}
check_url
3.expr命令使用:判断文件类型
#!/bin/bash
if [ `expr $1 ":" ".*\.png"` -gt 0 ]
then
echo "该文件是png文件"
exit 0
else
echo "该文件不是png文件"
exit 2
fi
4.一个安装lnmp、lamp的脚本
#! /bin/bash
#lujing
path=/all_test/install_scripts/
#判断目录是否存在 不存在则创建
[ ! -d "$path" ] && mkdir -p $path
# 给用户输入提示信息
cat <<END 1.[install lamp] 2.[install lnmp] 3.[exit] please input the number want: END
# 读取用户输入的值
read num
# 判断用户是否输入的数字
#test -n "$(${num}|sed 's/[0-9]//g')" && echo 'must to be number'
expr $num + 1 &> /dev/null
## 判断上句结果
[ $? -ne 0 ] && {
echo 'must to be number!'
}
# 对输入是1、2、3的情况进行处理
[ $num = 1 ] && {
echo "start installing...lamp.."
sleep 3
test -x "$path/lamp.sh" || {
echo "the file err..."
exit 1
}
$path/lamp.sh
exit $?
}
[ $num = 2 ] && {
echo "start installing...lnmp..."
sleep 2
[ -x "$path/lnmp.sh" ] || {
echo "the flie2 err..."
exit 2
}
$path/lnmp.sh
exit $?
}
[[ $num -eq 3 ]] && {
echo "bye"
exit 3
}
# 限制用户只能输入123
[[ ! "$num" =~ [1-3] ]] && {
echo "please input 1|2|3"
exit 4
}
边栏推荐
- [Database and SQL study notes] 8. Views in SQL
- 读论文 - Unpaired Portrait Drawing Generation via Asymmetric Cycle Mapping
- Detailed explanation of BroadCast Receiver (broadcast)
- 《基于机器视觉的输电线路交叉点在线测量方法及技术方案》论文笔记
- Mysql-连接https域名的Mysql数据源踩的坑
- 【数据库和SQL学习笔记】3.数据操纵语言(DML)、SELECT查询初阶用法
- CH32V307 LwIP移植使用
- Oracle压缩表修改字段的处理方法
- 关于存储IOPS你必须了解的概念
- 【Pytorch学习笔记】11.取Dataset的子集、给Dataset打乱顺序的方法(使用Subset、random_split)
猜你喜欢
![[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)](/img/ac/884d8aba8b9d363e3b9ae6de33d5a4.png)
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)

Mesos learning
![[Practice 1] Diabetes Genetic Risk Detection Challenge [IFLYTEK Open Platform]](/img/10/7aa3153e861354178f048fb73076f7.png)
[Practice 1] Diabetes Genetic Risk Detection Challenge [IFLYTEK Open Platform]

It turns out that the MAE proposed by He Yuming is still a kind of data enhancement

MaskDistill - Semantic segmentation without labeled data

Kubernetes常备技能

关于存储IOPS你必须了解的概念
![[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers](/img/b9/06b90160c962a25a3cc44731afb6dc.png)
[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers

flink项目开发-flink的scala shell命令行交互模式开发

网络信息安全运营方法论 (上)
随机推荐
MySQL
【数据库和SQL学习笔记】4.SELECT查询2:排序(ORDER BY)、聚合函数、分组查询(GROUP BY)
SSL 证书签发详细攻略
【数据库和SQL学习笔记】6.SELECT查询4:嵌套查询、对查询结果进行操作
Thread handler handle IntentServvice handlerThread
Thread handler句柄 IntentServvice handlerThread
物联网:LoRa无线通信技术
You should write like this
基于Flink CDC实现实时数据采集(三)-Function接口实现
【Pytorch学习笔记】11.取Dataset的子集、给Dataset打乱顺序的方法(使用Subset、random_split)
【论文精读】ROC和PR曲线的关系(The relationship between Precision-Recall and ROC curves)
[Skill] Long-term update
Web Component-处理数据
[Database and SQL study notes] 8. Views in SQL
如何跟踪网络路由链路&检测网络健康状况
吞吐?带宽?傻傻分不清楚
Flink Table API 和 SQL之概述
[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers
单片机按键开发库-支持连击、长按等操作
神经网络也能像人类利用外围视觉一样观察图像