Description of the Shell's conditional expressions

Source: Internet
Author: User

Determine if b.txt This file exists, there is output 1, there is no output 0

[-F B.txt]&& echo 1| | Echo 0

-F: Determines whether the file is

-E: Determine if the file exists

-D: Determine if the directory

-R: Judging whether it is readable

-W: Determine if writable

-X: Determine if executable


Double quotation marks are required to test a single file or directory variable to avoid errors

file=/etc/services[-F "$file"]&& echo 1| | Echo 0

Conditional expression judgment condition after executing multiple command statement wording

#!/bin/bash[$1-eq 2]&&{echo "true"}| | {echo "false"}# && executes the following statement after it is established; | | Do not set up to execute the following statement # If the input value equals 2 print true# otherwise print false#sh test.sh 2: Print True


Common String test operators:

-Z "string" True if string length is 0
-N "String" True if string length is not 0
"String 1" = "String 2" String 1 equals string 2 is True
"String 1"! = "String 2" String 1 is not equal to string 2 is True

Ps:

①, the string test operation symbol in the table above must be caused by ""

②, compare symbols must have spaces at both ends

#字符串长度为0所以输出1 [-N ""]&& echo 1| | Echo 0







Description of the Shell's conditional expressions

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.