Conditional testing of Shell programming

Source: Internet
Author: User

Condition test

(a) Concept: to determine the specific conditions to determine how to perform the operation, when the condition is set, the return value of the test statement is 0, otherwise, the other value, meaning if the echo $? The value is 0, then the condition is set. Classification of conditional tests: File test, Integer test, string test, logic test.

Test method:

Method 1:test Conditional expression

Method 2:[Conditional Expression]

(b) Test method:

1. File Testing

Format 1: [operator file or directory]

Format 2:test operator file or directory

Common Test operators

-D: Test for Catalog (directory)

-E: Test whether the directory or file exists (Exist)

-F: Test for file

-R: Tests whether the current user is readable (read)

-W: Tests whether the current user is writable (write)

-X: Test whether the current user is executable (excute)

1[[Email protected] ~]#[-d/media/cdrom] #注意这里中括号里需要和内容有空格2[[Email protected] ~]#echo $? # If the test condition is not true, the returned number is not 0314[[Email protected] ~]#[-d/root]5[[Email protected] ~]#echo $?6 07[[Email protected] ~]#test-d/root && echo "yes"8 "Yes"9 #the last statement means that if the previous condition test is established, then the output Yes, the application principle:&& is a ' logical and ' operator, the front is true, after the true, the entire statement is true, when the left is true, it will continue to trigger the logic and, Continue judging to the right to know if the whole statement is true, so you can output yes. 
2. Integer test

Format 1:[integer 1 operator integer 2]

Format 2:test integer 1 operator integer 2

Common Test operators

-eq: Equals (Equal)

-ne: Not equal to (not Equal)

-GT: Greater Than (Greater Than)

-lt: Less than (Lesser Than)

-le: Less than or equal to (Lesser or Equal)

-ge: greater than or equal to (Greater or Equal)

1 [[email protected] ~]#  who2 root tty1 2016-11-04 09:59 (: 0)3 Root pts/0 2016-11-04 09:59 (: 0.0)4 [[email protected] ~]# who | wc-l5 2
   
    6 [[email protected] ~]
    # 
     [$ (who | wc-l)-GT 1] && echo ' too many. ' 
    7 too many.
   
3. String test

Format 1:[String 1 = string 2]

[String 1! = String 2]

Format 2:[-Z string]

Common Test operators

=: string content is the same

! =: string content is different

-Z: string content is empty

1[[Email protected] ~]#[AAA = AAA] #判断两个字符串是否相等2[[Email protected] ~]#echo $?3 04[[Email protected] ~]#[AAA! = AAA]5[[Email protected] ~]#echo $?617[[Email protected] ~]#[-Z '] #引号里没有字符串, empty8[[Email protected] ~]#echo $?9 0Ten[[Email protected] ~]#[-Z ' AAA '] #引号里有字符串, not empty One[[Email protected] ~]#echo $? A1
1[[Email protected] ~]#read-p "Please enter user name:" Name #提示用户输入字符串 and assign to the name variable2Please enter user name:#user returns directly, no input3[[Email protected] ~]#[-Z $name] #判断name变量是否为空4[[Email protected] ~]#echo $?50#name is empty, judgment set, show 06[[Email protected] ~]#read-p "Please enter user name:" Name7 Please enter user name: Mr.mangood8[[Email protected] ~]#[-Z $name]9[[Email protected] ~]#echo $?Ten1
1 [[email protected] ~]#  echo $LANG                                 #查看 LANG language variable 2 zh_cn. UTF-83 [[email protected] ~]#  [$LANG! = "en. US "] && echo" not en. US "   #如果LANG不为en. us, then the output hints, the study is also the use of the $ variable 4 not en. US
4. Logic Test

Format:

[expression] operator [expression 2] ...

Command 1 operator Command 2 ...

Commonly used operators (note:-A and-o are placed in [] with,&& and | | Put in [] outside for use)

-A or && logic and

-O or | | Logical OR

! Logical No

1 [[email protected] ~]#  [-d/etc] && [-r/etc] && echo "You can open it"
   
    2
    You can open it
    3 [[email protected] ~]
    #
    
    4 OK
   
 1  [[email protected] ~]#   [-d/etc] | | echo " 2  [[email protected] ~]#   echo $?                                                                        #左侧为真, do not perform the right side of the echo section  3  0 #   Whole is true, so output 0  
1 [[email protected] ~]#  [2-eq 2-a 3-eq 4] && [-d/etc]2 [email Protec Ted] ~]#  echo $? 3 145 [[email protected] ~]#  [2-eq 2-o 3-eq 4] && [-D/ETC] C11>6 [[email protected] ~]#  echo $? 7 0

Conditional testing of Shell programming

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.