Shell basic knowledge 2

Source: Internet
Author: User

Conditional expression:

1. File expression

# If the file exists if [-f file] # if the directory exists if [-d dir] # if the file exists and is not empty if [-s file] # if the file exists and is readable if [-r file] # if the file exists, you can write if [-w file] # if the file exists, you can run if [-x file]

2. Integer expression

# If a is equal to bif [a-eq B] # if a is not equal to bif [a-ne B] # if a is greater than bif [a-gt B] # if a is greater than or equal to bif [a-ge B] # if a is less than bif [a-lt B] # if a is less than or equal to bif [a-le B]

3. string variables

A = 'B = ''# if a is equal to bif [$ {a }=$ {B}] # if a is not equal to bif [$ {}! =$ {B}] # if a is not blank if [-n $ {a}] # if a is blank, the length is 0if [-z $ {a}]

 

= Put in if is the equivalent of a string, and assign values elsewhere. When equal signs are used as values and equals, there is a lot of difference in writing: there cannot be spaces on both sides of the equal signs. equal to equal signs, there must be spaces on both sides.

The method used to judge whether the string is not null is incorrect. However, this is true for many materials (not recommended ). The following method is tested:

If [$ {a}] is null, else is used.

4. logical expressions

4.1. Non-if [! Expression]

4.2.

If [expression-a expression] Or if [expression] & [expression]

 

echo 'write path:'read pathif [ -d ${path} -a -n ${path} ]then  echo '---'else echo '+++'fi

Determines whether the input is a directory and not a blank string
Execution result :---

4.3, or

If [expression-O expression] Or if [expression] | [expression]

 

 

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.