当前位置:网站首页>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
边栏推荐
- leetcode-6132: Make all elements in array equal to zero
- 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
- 堆内存的介绍及应用(含例子)
- YOLOv7-Pose尝鲜,基于YOLOv7的关键点模型测评
- 2022杭电中超杯(1)个人题解
- 安装GBase 8c数据库的时候,报错显示“Resource,如何解决?
- 网络基础学习
- 走进音视频的世界——mp3封装格式
- Leicester Weekly 304 6135. The longest ring in the picture Inward base ring tree
- pytest interface automation testing framework | pass in parameter values in the form of function return values
猜你喜欢
随机推荐
Microsoft Azure & NVIDIA IoT 开发者季 I|Azure IoT & NVIDIA Jetson 开发基础
Intensive reading of ACmix papers, and analysis of its model structure
Microsoft Azure & NVIDIA IoT developers season I | Azure IoT & NVIDIA Jetson development foundation
云原生FAQ
STM32个人笔记-嵌入式C语言优化
【Untitled】
将aof文件转换为命令waoffle安装和使用
Static Pod, Pod Creation Process, Container Resource Limits
SaaS安全认证综合指南
MySQL query advanced - from the use of functions to table joins, do you remember?
[Beyond programming] When the fig leaf is lifted, when people begin to accept everything
HoloView--live data
How to ensure the consistency of database and cache data?
zip package all files in the directory (including hidden files/folders)
微服务:事务管理
Analysis of High Availability Solution Based on MySql, Redis, Mq, ES
常见的API安全缺陷有哪些?
node 格式化时间的传统做法与高级做法(moment)
HoloView -- Tabular Datasets
GBase 8c中怎么查询数据库配置参数,例如datestyle








