Condition Test in Shell

Source: Internet
Author: User

1.test statement

The test instruction is used when I want to detect that some of the file rings on the system are related attributes.

(1) The file type judgment about a filename, such as TEST-E filename indicates existence

-e Does the file name exist? Common

-F does the filename exist and is a file (file)? Common

-D does this "filename" exist and is directory (directory)? (common)-B does the "filename" exist and is a block device device?

-C Does the "filename" exist and is a character device device?

-S does the "filename" exist and is a Socket file?

-P does the filename exist and is a FIFO (pipe) file?

-L does the filename exist and is a link file?

(2) About the permissions of the file detection, such as test-r filename to indicate whether or not (but root permissions are often exceptional)

-R detects if the filename exists and has "read-only" permissions?

-W Detect if the filename exists and has "writable" permissions?

-X detects if the filename exists and has "executable" permissions?

-U detects the existence of the file name and has a "SUID" attribute?

-G detects if the filename exists and has "SGID" properties?

-K detects if the filename exists and has a "Sticky bit" attribute?

-S detects if the filename exists and is "not a blank file"?

(3) Comparison between two documents test File1-nt File2

-nt (newer than) determines whether file1 is newer than file2

-ot (older than) judge whether File1 is older than file2

-ef judge whether File1 and File2 are the same files, can be used to judge hard Link's judgment.

(4) A determination between two integers, such as test n1-eq N2

-eq two values equal (equal)

-ne two values unequal (not equal)

-GT N1 greater than N2 (greater than)

-lt N1 less than N2 (less than)

-ge N1 is greater than or equal to N2 (greater than or equal)

-le N1 is less than or equal to N2 (less than or equal)

(5) The data of the decision string

Test-z string evaluates to 0? True if string is an empty string

Test-n string Determines whether the string is not 0? False if string is an empty string. Note:-n can also be omitted

Test STR1 = str2 determine if str1 equals str2, if equal, return True

Test str1!= str2 determine if str1 is not equal to str2, if equal, return False

(6) Multiple condition determination, for example: Test-r filename-a-x filename

Both the status of-a (and) are simultaneous! For example, Test-r file-a-x file, the file has both R and X permissions to return True.

-O (or) two conditions any one is established! For example, Test-r file-o-x file, the file can return true if it has R or X permissions.

! Reversed phase state, such as test! -x file, which is true when file does not have an X.


# 1. Let the user enter the filename and determine if the user really has an input string?

ECHO-E "Please input a filename, I'll check the filename ' s type and

Permission. nn

Read-p "Input a filename:" filename

Test-z $filename && echo "You must input a filename." && exit 0

# 2. To determine if a file exists? display a message end script if it does not exist

Test! -E $filename && echo "The filename ' $filename ' do not exist ' &&

Exit 0

# 3. Start to determine file types and attributes

Test-f $filename && filetype= "Regulare file"

test-d $filename && filetype= "directory"

Test-r $filename && perm= "readable"

Test-w $filename && perm= "$perm writable"

Test-x $filename && perm= "$perm executable"

# 4. Start outputting information!

echo "The filename: $filename is a $filetype"

echo "and the permissions are: $perm"

2. Using the judgement symbol []

In addition to the test we like to use, in fact, we can often judge the symbol [] (that is, the brackets) to judge the data! But pay attention to the following points:

(1) Each component in the brackets [] is required to be separated by a space bar;

(2) The number of the hair within the brackets, preferably with double quotation marks in parentheses;

(3) The constants within the brackets, preferably enclosed in single or double quotes.




Export PATH

Read-p "Please input (y/n):" yn

["$yn" = = "Y"-o "$yn" = "y"] && echo "OK, continue" && exit 0

["$yn" = = "n"-o "$yn" = = "n"] && echo "Oh, interrupt!" && exit 0

echo "I don ' t know what your choice is" && exit 0

Several constants in the that are often used in the test

(1) The execution of the script file named $ this variable, the first argument is $, and so on.

(2) $#: Represents the "number" of the following parameters, the $ $ is not counted in it.

(3) $@: Representing "$" "$" "$" "$" means that each variable is independent (enclosed in double quotes).

(4) $*: Represents "" $1c$2c$3c$4 "", where c is the separator character, the default is the space bar, so this example represents "$ $ $ $" meaning.


The script is as follows:

echo "The script name is ==> $"

echo "Total parameter number is ==> $#"

["$#"-lt 2] && echo "The number of parameter is less than 2. Stop

here. "

&& Exit 0

echo "Your whole parameter is ==> ' $@ '"

echo "the 1st parameter ==> $"

echo "the 2nd parameter ==> $"

Execution results:

[root@www scripts]# sh theone haha quot

The script name is ==> <== file name

Total parameter number is ==> 3 <== three parameters

Your whole parameter is ==> ' theone haha quot ' <== ' all contents of parameters

The 1st parameter ==> theone <== first parameter

The 2nd parameter ==> haha <== second parameter

Note: You can also use shift for parameter offset

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.