Shell Learning [test Command]
Shell test command
The test command is mainly used to check whether the condition is established, and can be used for numeric judgment, character judgment and file judgment; you can also use your own commonly used check commands.
Test Digital Demo #! / bin/bashecho "Hello World!" num1=10num2=10# routinely judges if [${num1}-eq ${num2}] then echo 'two numbers are equal!' Else echo 'two numbers are not equal!' Fi#test routinely judges that if test $[num1]-eq $[num2] then echo 'two numbers are equal!' Else echo 'two numbers are not equal!' Fi
The numeric judge parameter states that-eq equals to true (abbreviation of equal)-ne is not equal to true (abbreviation of not equal)-gt is greater than or equal to true (abbreviation of greater than)-ge is greater than or equal to true (abbreviation of greater&equal)-lt is less than or equal to true (abbreviation of lower than)-le less than or equal to true (abbreviation of lower&equal)
Test character demonstration #! / bin/bashnum1= "aaaa" num2= "bbbb" # routine judgment if [${num1} = ${num2}] then echo 'character equality!' Else echo 'characters are not equal!' Fi#test judges if test $num1 = $num2then echo 'character equality!' Else echo 'characters are not equal!' Fi
The character judge parameter description = equal is true! if = is not equal, it is true-z string length is zero, then true-n string string length is not zero, it is true
The test file demonstrates whether the #! / bin/bash# regular file exists if [!-d $BAK_DIR]; then mkdir-p $BAK_DIRfi#test determines that the file if test-e ${BAK_DIR} then echo 'file exists!' Else echo 'file does not exist!' Fi# and (- a), or (- o), and non (!) logical operators are used to if test-e ${BAK_DIR}-o-e ${BAK_DIR1} then echo'at least one file!' Else echo'no file exists!' Fi
The file judge [test] parameter states that the file name is true if the file exists-r file name if the file exists and is readable, true-w file name if the file exists and writable, true-x file name if the file exists and executable, true-s file name if the file exists and has at least one character, true-d file name if the file exists and is a directory, true-f The file name is true if the file exists and is a normal file
Note: three logical operators, namely (- a), or (- o) and non (!), are used to connect test conditions with a priority of: "!" The highest, the second is "- a" and the lowest is "- o".