当前位置:网站首页>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
边栏推荐
- 静态Pod、Pod创建流程、容器资源限制
- Chapter 9 of Huawei Deep Learning Course - Convolutional Neural Network and Case Practice
- GBase 8c中怎么查询数据库配置参数,例如datestyle
- What do the values 1, 2, and 3 in nodetype mean?
- sqlserver怎么查询一张表中同人员的交叉日期
- Leicester Weekly 304 6135. The longest ring in the picture Inward base ring tree
- ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API
- XX市消防救援指挥中心实战指挥平台多链路聚合解决方案实例
- Shell executes SQL to send emails
- 热修复技术可谓是百花齐放
猜你喜欢
随机推荐
leetcode-6134:找到离给定两个节点最近的节点
How to ensure the consistency of database and cache data?
Shell executes SQL to send emails
HoloView 在 jyputer lab/notebook 不显示总结
实验。。。。
报告:想学AI的学生数量已涨200%,老师都不够用了
The soul asks: How does MySQL solve phantom reads?
Leetcode - 6135: the longest part of the figure
扁平数组转树结构实现方式
In the background of the GBase 8c database, what command is used to perform the master-slave switchover operation for the gtm and dn nodes
How to query database configuration parameters in GBase 8c, such as datestyle
MySQL query advanced - from the use of functions to table joins, do you remember?
优炫数据库支持Oracle哪几种时间及日期类型
[Interview: Concurrency 39: Multithreading: Thread Pool] ThreadPoolExecutor Class - Submit, Stop
UXDB如何返回当前数据库所有表的记录数?
Data Analysis 5
笔记。。。。
自定义IP在PCIE中使用
Mysql database deployment and initialization steps
[Dataset] Dataset summary of various insulators, bird's nests and anti-vibration hammers








