Shell conditional test command

Source: Internet
Author: User
Shell conditional test Command 1. test statement when I want to check that some of the above file scammers are related attributes, I need to use the test command. (1) determine the file type of a file name. for example, test-efilename indicates that-e exists and whether the file name exists... information & n shell condition Test Command 1. the test statement must use the test command when I want to check that some of the above-mentioned file scammers are related attributes. (1) determine the file type of a file name. for example, test-e filename indicates that-e exists. Does the file name exist? (Commonly used)-f does this file name exist and is it a file )? (Commonly used)-d does the "file name" exist in a directory )? (Commonly used)-B does the "file name" exist and is a block device? -C does the file name exist and is a character device? -S does the "file name" exist and is a Socket file? -P: does this file name exist and is a FIFO (pipe) file? -L does the "file name" exist and is a link file? (2) Check the permission of an archive. for example, test-r filename indicates whether the file is readable (except for root permission). does r detect whether the file name exists and has the "readable" permission? -W: Does the file name exist and has the "writable" permission? -X checks whether the file name exists and has the "executable" permission? -U checks whether the file name exists and has the "SUID" attribute? -G checks whether the file name exists and has the "SGID" attribute? -K checks whether the file name exists and has the "Sticky bit" attribute? -S: Does the specified file name exist and it is a non-blank file 』? (3) comparison between two documents test file1-nt file2-nt (newer than) to determine whether file1 is newer than file2-ot (older) determine whether file1 is earlier than file2-ef determines whether file1 and file2 are in the same file. it can be used to determine hard link. (4) determination of two integers. for example, the two values of test n1-eq n2-eq are equal (equal)-ne and not equal (not equal) -gt n1 greater than n2 (greater than)-lt n1 less than n2 (less than)-ge n1 greater than or equal to n2 (greater than or equal) -is le n1 less than or equal to n2 (less than or equal) (5? If string is a null string, true test-n string is used to determine whether the string is not 0? If string is a null string, it is false. Note:-n can also omit test str1 = str2 to determine whether str1 is equal to str2. if it is equal, return true test str1! = Str2: determines whether str1 is not equal to str2. if it is equal, Multiple Criteria of false (6) are returned. for example: test-r filename-a-x filename-a (and) both cases are true! For example, if test-r file-a-x file has both r and x permissions, true is returned. -O (or! For example, if test-r file-o-x file has the r or x permission, true can be returned.! Reverse state, such as test! -X file: Returns true if file does not have x. Example: [html] #1. let the user enter the file name and determine whether the user actually has an input string? Echo-e "Please input a filename, I will check the filename's type and \ permission. \ n "read-p" Input a filename: "filename test-z $ filename & echo" You MUST input a filename. "& exit 0 #2. determine whether an archive exists? If the message does not exist, the end script test is displayed! -E $ filename & echo "The filename '$ filename 'do NOT exist" & exit 0 #3. start to judge the file type and attribute 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 to output information! Echo "The filename: $ filename is a $ filetype" echo "And the permissions are: $ perm" 2. using the judgment symbol [] in addition to the test we like very much, we can also often use the judgment symbol [] (that is, brackets) for data judgment! Note the following: (1) each component in the brackets [] must be separated by a space key; (2) the number of rounds in the brackets, it is best to enclose them in double quotation marks. (3) constants in the brackets. it is best to enclose them in single or double quotation marks. Example: [html] #! /Bin/bash PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin 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 3. several Constants (1) that are often used in the test in shell run the script file named $0. The first parameter is $1, and so on. (2) $ #: indicates the number of connected parameters. $0 is not included. (3) $ @: represents the meaning of "" $1 "" $2 "" $3 "" $4 "". each variable is independent (enclosed in double quotation marks ). (4) $ *: indicates "" $ 1c $ 2c $ 3c $4 "", where c is a delimiter, and the default value is the space key, therefore, this example indicates "$1 $2 $3 $4. Example: [html]
The script is as follows: echo "The script name is ==>0 0" echo "Total parameter number is ==>$ #" ["$ #"-lt 2] & echo "The number parameter is less than 2. stop here. "\ & exit 0 echo" Your whole parameter is ==>' $ @ '"echo" The 1st parameter ==>1 1 "echo" The 2nd parameter ==>$ 2 "execution result: [root @ www scripts] # sh sh07.sh theone haha quot The script name is ==> sh07.sh <=? N Total parameter number is ==> 3 <== there are three parameters: Your whole parameter is ==> 'theone haha quot '<= all The content of The parameter The 1st parameter ==> theone <= The 2nd parameter => haha <= The second parameter


 

Note: shift can also be used for parameter offset.

 
 
Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.