The test command usage tutorial in Linux shell

  • 2020-05-30 21:58:38
  • OfStack

test command

The test command is a utility for testing conditional expressions in an shell environment. Here's how to use the test command:

grammar

test (option)

options

-b < file > : true if the file is 1 block special file; -c < file > : true if the file is a 1-character special file; -d < file > : true if the file is 1 directory; -e < file > : true if the file exists; -f < file > : true if the file is a normal file; -g < file > : true if the SGID bit of the file is set; -G < file > : true if the file exists and belongs to the group; -k < file > : true if the sticky bit of the file is set; -O < file > : true if the file exists and belongs to the user; -p < file > : if the file is a named pipe, it is true; -r < file > : true if the document is readable; -s < file > : true if the length of the file is not zero; -S < file > : true if the file is a socket special file; -u < file > : true if the SUID bit of the file is set; -w < file > : true if the document is writable; -x < file > : true if the file is executable.

The instance

Common usage of test in shell programming in linux:

Judgment expression


if test  # The expression is true 
if test ! # The expression is false 
test  expression 1  � a  expression 2  # Both expressions are true 
test  expression 1  � o  expression 2  # Two expressions are 1 A true 
test  expression 1 !  expression 2  # Conditions of complementation 

Judgment string


test  � n  string  # The length of a string is non-zero 
test  � z  string  # Whether the length of the string is zero 
test  string 1 = the string 2  # Whether the strings are equal, if so, return true
test  string 1! = the string 2  # String is not equal, if not, back false

To determine the integer


test  The integer 1 -eq  The integer 2 # An integer equal 
test  The integer 1 -ge  The integer 2 # The integer 1 Greater than or equal to an integer 2
test  The integer 1 -gt  The integer 2 # The integer 1 Is greater than the integer 2
test  The integer 1 -le  The integer 2 # The integer 1 Less than or equal to an integer 2
test  The integer 1 -lt  The integer 2 # The integer 1 Less than an integer 2
test  The integer 1 -ne  The integer 2 # The integer 1 Not an integer 2

Judge documents


test File1  � ef File2  Whether the two files are the same 1 Can be used for hard connection. The main judge whether the two files point to the same 1 a inode . 
test File1  � nt File2  Judge documents 1 Whether to compare the file 2 new 
test File1  � ot File2  Judge documents 1 Than whether file 2 The old 
test  � b file # Whether the file is a block device file 
test  � c File # The file is also a character device file 
test  � d File # The file is also a directory 
test  � e File # Does the file exist   (common) 
test  � f File # Whether the document is a formal document   (common) 
test  � g File # Whether the file is set to a group id
test  � G File # The file belongs to a valid group ID
test  � h File # Is the file 1 Symbolic link (same as -L ) 
test  � k File # Whether the file is set Sticky bit position 
test  � b File # The file exists and is a block device file 
test  � L File # Is the file 1 Symbolic link (same as -h ) 
test  � o File # The file belongs to a valid user ID
test  � p File # The file is 1 Individual named pipes 
test  � r File # Whether the file is readable 
test  � s File # Whether the file is a non-blank file 
test  � t FD  # The file descriptor is in 1 The terminal is open 
test  � u File # The file exists and sets its set-user-id position 
test  � w File # Whether the file exists and is writable 
test  � x File # Whether the file exists and is executable 

expand

test xxx can be reduced to the form [xxx].

Note: when using the "[" abbreviation test, the space after the left middle parenthesis and before the right parenthesis is required. Without a space, Shell cannot tell when the expression begins and ends.

That is to say,


 test option file

They can all be rewritten as:


 [ option file ]

Such as:


 test  � w File

Rewrite into


[  � w File ] 

[example]


// Determine the first 1 Whether the parameters are empty string, not empty print 
if test -n "$1"
then
echo "$1"
fi

Test it, put it in a file


#!/bin/sh
if test -n "$1"
then
echo "$1"
fi
 perform 
chmod +x test.sh
./test.sh www.linuxpig.com

conclusion


Related articles: