Linux Bash Shell Programming (8): Conditional Judgment and Examples

Linux Bash Shell Programming (8): Conditional judgment statements and examples

  In the previous section , we learned about the relevant commands for string interception and processing, and can filter and intercept the output data stream of the command. At the beginning of this section, it is the most critical part of Bash Shell programming: conditional judgment and flow control statements. In this section, we are first familiar with the various types and forms of conditional judgment.

Bash 8

Basic grammar of conditional judgment

  There are mainly two basic syntaxes for conditional judgment: command-line-based testcommands and conditional styles that are more suitable for embedding process control statements.

  1. The test command
      runs a conditional judgment statement in the form of a command line without any standard output. The execution result of the statement is saved in a predefined variable $?( note that the $? value is similar to the return value of the program, not a boolean data , $? 0 is true, 1 is false ), can be viewed directly by echocommands, or called by multiple command logical relations, which is more in line with the user’s habit, but it is inconvenient to use in Shell scripts. Generally, it is not recommended to use in Shell
test 条件判断语句
#为了观察实际效果,一般使用echo命令查看$?预定义变量
echo $?
#0为真,1为假

#例如:(判断语句的具体分类在下面讲到)
test -e Hello_World.sh
echo $?
  1. With []conditional styles included

      The function of using the []included condition to judge the expression is the testsame as the command. It does not adopt the imperative structure. It can be easily embedded in the process control statement as a condition. The return value is also in the predefined variable $?. Except for the different form, the rest is testexactly the same as the command

    Note: [] must have spaces on both sides of the inside, otherwise an error will be reported when the command is executed

[ 条件判断语句 ]
#同样使用echo命令查看条件判断语句结果
echo $?
   
#例如:(判断语句的具体分类在下面讲到)
[ -e Hello_World.sh ]
echo $?

Conditional judgment statement type

Judging by file type

Add the file name after the option to determine whether the file type (if it exists) meets the conditions. If the file does not exist, it will return false, and if it meets the conditions, it will return true. The specific file type identifier is similar to the first one of the file attribute in the ls command result.

[选项 文件名]
#echo #?
Options effect
-b Determine whether the file (if it exists (the same below)) is a block device file (is true (the same below))
-c Determine whether the file is a character device file
-d Determine whether the file is a directory file
-e exist only judges whether the file exists
-f file Judge whether the file is a normal file
-L Determine whether the file is a symbolic link file
-p Determine whether the file is a pipeline file
-s Determine whether the file is not empty (not empty is true)
-S Determine whether the file is a socket file

Judging by file permissions

Also add the file name after the option, if the permission is true, if there is no permission or the file does not exist, it is false. (Judging is the current user's authority to this file)

Options effect
-r Determine whether the file has read permission
-w Determine whether the file has write permission
-x Determine whether the file has execution permission
-u Determine whether the file has SUID permissions
-g Determine whether the file/directory has SGID permissions
-k Determine whether the directory has SBIT permissions

Compare the information of two files

Format: File 1 [Option] File 2

Options effect
-nt newer than judges whether the last modification time of file 1 is later than file 2, and later is true (0)
-ot older than Judge whether the last modification time of file 1 is earlier than file 2, which is true (0)
-ef Determine whether the Inode numbers of the two files are the same, which is similar to whether the two files are the same
  • Among them, the Inodenumber is the serial number that identifies the file. Two different files can have the same name, but the Inode number cannot be the same. If the two files are hard-linked (save as a copy, synchronized update) relationship, the Inode number of the two files will be the same , The rest are different
  • Two hard-linked files have no signs except for the same Inode number. You can use this judgment method to judge the hard link

Compare two integers

If it is two variables, it does not need to be declared in integer format, syntax: integer 1 [option] integer 2

Options effect
-eq equal Determine whether two integers are equal
-born not equal Determine whether two integers are not equal
-gt greater than judge whether the integer 1 is greater than the integer 2
-lt less than judge whether the integer 1 is less than the integer 2
-give greater/equal judge whether the integer 1 is greater than or equal to the integer 2
-the less/equal judge whether the integer 1 is less than or equal to the integer 2

Determine string correlation

The grammar is included in the options. Note: The comparison of the two strings must have spaces on both sides of the relevant predicate, otherwise the return value will be true anyway

Options effect
-z string Determine whether the string is empty
-n string Determine whether the string is not empty
String 1 == string 2 Determine whether two strings are equal
String 1 != string 2 Determine whether two strings are not equal
  • When the string is a variable, if the variable is not defined, it is also treated as an empty string

The logical relationship between the two judgment conditions

When using two judgment conditions at the same time, pay attention to the logical relationship between them, the format of the two judgment conditions: condition 1 [option] condition 2

Options effect
-a Logical AND, both conditions are true
-The Logical OR, if one of the two conditions is true
! [Conditions] Logic negation, invert the judgment result ( note that !there is a space between the condition and the condition )

Conditional judgment statement used in conjunction with multiple command logic symbols

  For the sequence of multiple commands and logical characters, please refer to Linux Bash Shell Programming (3): redirection, multiple command sequences, wildcards, special symbols

  In fact, the way to judge whether the previous command is executed by multiple command sequences and logic symbols is to judge whether $?the value of the predefined variable is 0, so you can directly treat the conditional judgment statement as a command and add it to the multiple command sequence statement, and the conditional judgment is true Equivalent to the correct execution of the previous statement.


Example

Example 1. Determine whether the file exists:

[ -e ./test.sh ] && echo "File exists" || echo "File not found"
#条件判断语句方括号内侧必须有空格
#该命令执行,若文件存在,显示"File exists";否则显示"File not found"

Example 2: Use multi-command logic to construct a single-line ratio program:

#以下为test5.sh中的内容
#!/bin/bash

#Author:Zheng

read -p "a= " a
read -p "b= " b #通过键盘读入a,b数据,请参阅(四)
[ $a -lt $b ] && echo "a<b" || echo "a>=b"

Example 3: Multi-condition judgment statement: judge whether the input variable is empty and the value is less than 10

#以下为test6.sh中内容
#!/bin/bash

#Author: Zheng

read -p "Input a: " a
#若a非空且小于10,则输出Yes
[ -n $a -a $a -lt 10 ] && echo "Yes" || echo "No"
#此脚本中未加入判断a是否为整数的条件,留待改进

index

In the next section, Linux Bash Shell Programming (9): Flow Control Statement (Part 1) Branch Statement (if, case) , we will learn Bash flow control statement

The last section, Linux Bash Shell programming (7): string interception and processing (cut, printf, awk, sed)

Guess you like

Origin blog.csdn.net/Zheng__Huang/article/details/108025604