Shell Note 7

Source: Internet
Author: User
Tags first string logical operators

  1. The result of the test is 0 or not 0, 0 is true, and not 0 means false.

  2. Test command

    Typically used with statements such as if, while, until, and so on.

    #test An expression

    #test [expression]//[trailing space,] preceded by a space

  3. File properties Test Expression


    An expression Description
    -B File True if the file exists and is a block device (block speicial)
    -C file True if file exists and is a character device (Character special)
    -R File True if file exists and is read-only
    -W File True if file exists and can be written to
    -X File True if file exists and can be executed
    -S file The value is true if the file exists and is longer than 0
    -D File If file is a directory, the value is True
    -F File If the file is a normal file, the value is True
    -E File If the file exists, the value is True
  4. Test example


  5.   1 #! /bin/bash  2  #filename: Filee   3 echo  "Please enter the file name:"   4 read  filename  5 if test -e  $FILENAME  ; then  6 ls -l   $FILENAME   7 else  8 touch  $FILENAME   9 fi 
  6. Test values


    Options Description
    N1-eq N2 N1 equals N2, then true
    N1-ne N2 N1 is not equal to N2, it is true
    N1-GT N2 N1 is greater than N2, it is true
    N1-lt N2 N1 is less than N2, it is true
    N1-ge N2 True if N1 is greater than or equal to N2
    N1-le N2 N1 is less than or equal to N2, true
  7. Test example

  8.   1 #! /bin/bash  2  #filename: Filee   3 echo  "Please enter the first number:"   4 read  n1  5 echo  "Please enter the second number:"   6 read  N2  7 if test  $N 1 -eq  $N 2 ; then  8 echo   "N1 == n2"   9 else 10 echo  "N1 != n2"  11 fi 
  9. Test string


    Options Description
    -Z S1 If the length of the string S1 is 0, the value is True
    -N S1 If the length of the string S1 is not 0, the value is True
    S1=s2 If the string S1 is equal to the string s2, the value is True
    S1!=s2 The value is true if the string S1 is not equal to the string s2
    S1 If the string S1 is not an empty string, the value is True
  10. Test example


  11.   1 #! /bin/bash  2  #filename: Filee   3 echo  "please enter the first string:"   4 read  s1  5 echo  "please enter the second string:"   6 read  S2  7 if test  $S 1 =  $S 2 ; then  8 echo  "S1 == s2"   9 else 10 echo  "S1 != s2"  11 fi 
  12. Test logical operators


    logical operators Description
    -A Binary "and" operators
    -O Binary "or" operator
    ! Unary "non" operator
  13. Test example


  14. 1 #!   /bin/bash 2 #filename: Filee 3 echo "Please enter the first string:" 4 read S1 5 echo "Please enter the second string:" 6 Read S2 7 if test $S 1 = $S 2-o $S 1 = "abc"; Then 8 echo "S1 = = S2" 9 Else ten echo "S1! = S2" one fi



Shell Note 7

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.