当前位置:网站首页>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 files
After 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
边栏推荐
猜你喜欢
随机推荐
CTO强烈禁止使用Calendar,那用啥?
巧妙利用unbuffer实时写入
优炫数据库支持Oracle哪几种时间及日期类型
Graduation thesis writing skills
leetcode-6133: maximum number of groupings
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
《时代》杂志:元宇宙时代将改变世界
微服务:事务管理
js中如何实现深拷贝?
[Dataset] Dataset summary of various insulators, bird's nests and anti-vibration hammers
sql server, FULL模式, dbcc shrinkfile(2,1) 不能收缩事务日志,还是原来的大小,是为什么?
Redis 3.2.3 crashed by signal: 11 服务宕机问题排查
Pod环境变量和initContainer
USB Protocol (2) Terminology
net stop/start mysql80 拒绝访问
HPC系统简介
淘宝商品详情又见淘宝详情,升级高级版 API
Is the real database data of TiDB stored in kv and pd?
SkiaSharp's WPF self-painted five-ring bouncing ball (case version)
Get the Token from the revised version of Qubutu Bed