当前位置:网站首页>Shell: Conditional test action
Shell: Conditional test action
2022-08-01 09:24:00 【IT.cat】
I. Grammar format:
1.1, text test:
Format 1 test conditional test type
Test 2 [conditional expression] #Note that there needs to be a space between the brackets and the expression or [[conditional expression]]
File test refers to judging whether the corresponding file or directory is corresponding to the given path name, or judging whether the file is readable, writable, executable, etc.The common operation options for file testing are as follows. When using, the test object can be placed after the operation option.-d: Test whether it is a directory (Directory).-e: Test whether the directory or file exists (Exist).-f: Test whether it is a file (File).-r: Test whether the current user has permission to read (Read).-w: Test whether the current user has permission to write (Write).-x: Test whether the execute permission is set.-b: test if it is a device file-c: test if it is a character device file-s: test exists and file size is empty-L: test for linked filesAfter the conditional test operation is executed, the return status value of the test command can be obtained through the predefined variable $?, so as to judge whether the condition is established.For example, execute the following operation to test whether the directory /media/ exists, if the return value $? is 0, it means the directory exists, otherwise it means it does not exist or it exists but is not a directory.

1.2, value comparison
Integer value comparison refers to judging the relationship between the first number and the second number based on two given integer values, such as whether it is greater than, equal to, or less than the second number.The common operation options for integer value comparison are as follows, when used, place the operation option between the two integers to be compared.Common test operators-eq: The first number is equal to (Equal) the second number.-ne: The first number is not equal to (Not Equal) the second number.-gt: The first number is greater than (Greater Than) the second number.-lt: The first number is less than (Lesser Than) the second number.-le: The first number is less than or equal to (Lesser or Equal) the second number.-ge: The first number is greater or equal to (Greater or Equal) the second number.
1.3, string comparison
String comparison is usually used to check whether the user input, system environment, etc. meet the conditions. In shell scripts that provide interactive operations, it can also be used to determine whether the positional parameters input by the user meet the requirements.Common operation options for string comparisons are as follows.=: The first string is the same as the second string.!=: The first string is not the same as the second string, where the "!" symbol means negation.-z: Check if the string is empty (Zero), treat variables that are not defined or assigned a null value as empty strings.
1.4, logic test
Logical testing refers to judging the dependencies between two or more conditions.When the system task depends on a number of different conditions, according to whether these conditions are established at the same time or only one of them is established, a testing process is required.Commonly used logic test operations are as follows, when used between different test statements or commands.● &&: Logical AND, which means “and”, only when the current and latter two conditions are established, the return value of the entire test command is 0 (the result is established).When using the test command to test, "&&" can be changed to "-a".● ||: Logical OR, indicating "or", as long as one of the two conditions before and after is established, the return value of the entire test command is 0 (the result is established).When testing with the test command, "||" can be changed to "-o".● !: Logical No, indicating "No", only when the specified condition is not satisfied, the return value of the entire test command is 0 (the result is true).
Second, if statement
if conditional test actionthencommand sequencefi #Note that there is an end at the end, the beginning and the end must be a pair, otherwise a syntax error will be reported#!/bin/bash
read -p "Please enter your score: " score
if [ $score -eq 100 ];then
echo "Shower!"
elif [ $score -ge 90 ] && [ $score -lt 100 ]
then
echo "$score, copy 10 times!"
elif [ $score -ge 70 -a $score -le 89 ]
then
echo "$score, copy 20 times!"
elif [ $score -ge 60 -a $score -le 69 ]
then echo "$score, copy 30 times!"
elif [ $score -ge 0 -a $score -lt 60 ]
then echo "$score, copy 30 times!"
else echo "Incorrect input!"
fi
边栏推荐
- Mysql database deployment and initialization steps
- 【杭电多校第四场 B题】最短路图+缩点dp
- 将aof文件转换为命令waoffle安装和使用
- Pytest | skip module interface test automation framework
- Do I need to introduce any dependencies to write data to clickhouse using flinksql?
- Redis中间件(从搭建到弃坑)
- SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
- Chapter 9 of Huawei Deep Learning Course - Convolutional Neural Network and Case Practice
- Analysis of High Availability Solution Based on MySql, Redis, Mq, ES
- Case practice --- Resnet classic convolutional neural network (Mindspore)
猜你喜欢
随机推荐
VoLTE Basic Learning Series | Enterprise Voice Network Brief
Microsoft Azure & NVIDIA IoT developers season I | Azure IoT & NVIDIA Jetson development foundation
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
The soul asks: How does MySQL solve phantom reads?
zip package all files in the directory (including hidden files/folders)
GBase 8s 锁分类
A problem with writing to the database after PHP gets the timestamp
STM32个人笔记-看门狗
HoloView 在 jyputer lab/notebook 不显示总结
HoloView -- Tabular Datasets
How to get page data
YOLOv7-Pose尝鲜,基于YOLOv7的关键点模型测评
codeforces每日5题(均1600)-第二十七天
net stop/start mysql80 拒绝访问
《时代》杂志:元宇宙时代将改变世界
程序员如何学习开源项目,这篇文章告诉你
JVM 运行时数据区与JMM 内存模型详解
【STM32】入门(二):跑马灯-GPIO端口输出控制
HoloView--Customization
正则表达式符号



![ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API](/img/b3/0167c22f14b97eb0206696495af7b5.png)





