Shell Programming Learning (six)

Shell Scripting condition test and compare

Review for conditions

It should be carried out in various conditions and the structure of the various control structures Bash test, and then performs different operations according to the test results, and if other conditions are sometimes combined statement, to complete the test is determined to reduce errors in the program run.

Test conditions common grammar

Grammar test conditions Explanation
Syntax 1: test <test expression> This is a test using the test command conditional expression methods. test command and at least one space between "<test expression>"
Syntax 2: [<test expression>] This is achieved by []the method conditional test expression. And test the same command usage, recommend this method. [] Boundaries and content have at least one space
Syntax 3: [[<test expression>]] This is achieved by [[ ]]the method conditional test expression. [[]]Boundaries and content have at least one space
Syntax 4: ((<test expression>)) This is achieved by (())the method of conditional expression test, generally used for ifstatements inside. (()) Does not require a space distal
  • Syntax 1 in testthe command syntax and 2 []are equivalent. Syntax 3 in [[]]double brackets for the expanded test command.
  • 4 is syntax (())used in the calculation.
  • In the double brackets [[]]may be used in a pattern matching the wildcard like, which is to distinguish it from several other places syntax.
  • &&, ||, >, <Other operators may be applied to double brackets {[[]]}but can not be applied to a single bracket [], the []generally used -a, , -o(for-gt integer), -lt(for integer) in place of the operator.
  • Integer arithmetic relationship may be used shell arithmetic operators (()).

Conditions practice test command

The simple syntax and examples of test conditions tested:

test条件测试的语法格式:test 条件表达式
Example 1: Test file file exists:
test -f file && echo true || echo false # 测试file文件是否存在,,如果存在则输出true,否则输出false
test -f file && echo 1 # 若表达式成功,则输出1
test -f file || echo 0 # 若表达式失败,则输出0
Example 2: the length of the test string is 0:
[root@www ~]# test -z "Miya" && echo true || echo false # 如果测试字符串的长度为0,则表达式成立,因为测试的字符串为Miya,不为0,则表达式的结果为false。
[root@www ~]# test -z "Miya" && echo 1 || echo 0
false

[] Brackets test conditions and sample grammar

语法格式:[ 条件测试表达式 ]   # 中括号两端要有空格
Example 1: Testing file existence:
[ -f file ] && echo 1 || echo 2 # 测试文件file是否存在,存在返回1,不存在返回0。

[[]] Syntax double brackets test conditions and sample

语法格式:[[ 条件测试表达式 ]]  # 双中括号里的两端也要有空格
[[ -f file ]] && echo 1 || echo 0 # 测试文件file是否存在,如存在则返回1,不存在返回0。

[DESCRIPTION] in [[ ]], you can use wildcards like pattern matching, and &&, ||, >, <and other operators can be used in [[ ]], but not applied [ ], the [ ]generally used -a, , -o(for-gt integer), -lt(for integer), etc. for the operator instead of the above-mentioned [[ ]]symbols. While [[]]the scenario is not much, but in [[]]the next scene wildcard matching, other test expression can not be replaced, or if you need a wildcard match on a regular match with [[]].

File test expression

Common file test operators Explanation
-d file, directory File exists and is a directory is true that the establishment of the test expression test
-f file, file File exists and is a regular file True
-e file, exist True file exists, -f, -e not identify a directory or file
-r file, read True-readable file exists and
-s file, size File exists and the file size is not 0 is true
-w file, write File exists and is writable True
-x file, executable True file exists and is executable
-L file, link File exists and is linked files True
f1 -nt f2,nt为newer than File 1 to File 2 new True
f1 -ot f2,ot为older than File 1 to File 2 Old True

Files practice test expression

Test file types

[ -f file ] && echo 1 || echo 0 # 文件为普通文件且存在则输出 1

Determine whether a file exists

[root@mico ~]# [ -f /etc/hosts ]
[root@mico ~]# echo $?
0
[root@mico ~]# [ -f /etc/hosts1 ]
[root@mico ~]# echo $?
1
判断文件是否存在,返回方式
[root@mico ~]# [ -f /etc/hosts ] && echo "文件存在" || echo "文件不存在" 
文件存在
[root@mico ~]# [ -f /etc/hosts1 ] && echo "文件存在" || echo "文件不存在" 
文件不存在

Determine whether a directory exists

[root@mico ~]# [ -d /tmp ] && echo "目录存在" || echo "目录不存在" 
目录存在
[root@mico ~]# [ -d /tmp1 ] && echo "目录存在" || echo "目录不存在" 
目录不存在

The determination method using variables

dir=/etc1/;[ -d $dir ] && tar zcf etc.tar.gz $dir || echo "$dir目录不存在"

[Description]

  • Read the test file, write, execute other attributes, not just based on file attributes rwxidentified by the judge, it depends on whether the user is currently performing the test can really operate in accordance with the corresponding file permissions.
  • Used [ ]when the test variable brackets, if the subject being tested is a variable, you will need to double quotes.

Special conditions of the test expression cases

The following conditions apply to all written test expression, the work is more commonly used alternative to the if statement.

Example 1: When the condition 1 is satisfied, while executing a command, the command 2, 3 commands:


Analyzing the above, the following effects corresponding to the if statement:

String test operators

Two yuan integer comparison operators

[Description]

  • When the "=" and "! =" May be used for comparison in [], but using in [] with ">" and "<" sign, needs to be escaped, sometimes escape, although not being given grammar , but the results may be wrong.
  • Symbols may also be used comprising "-gt" and "lt" in [[]], but it is not recommended to use.
  • Compare both ends of the symbol should have spaces.

Logical Operators

[Description]

  • "-A" and "-o" logical operators need for brackets [].
  • "&&" and "||" symbols available for logic operations [[]] and (()), a plurality of external connection may be [].
  • Note that the [ends] and [[]], there must be a space, but for (()) required.

to sum up

Test expression test, [], [[]], (()) the difference between Summary:

Guess you like

Origin www.cnblogs.com/vicodona/p/11212318.html