Linux Shell if [-n] Correct use method

Source: Internet
Author: User

if [str1 = str2] When two strings have the same content, the length is true
if [str1! = str2] When string str1 and str2 are not equal
If [-N str1] is true when the length of the string is greater than 0 (string non-null)
If [-Z str1] is true when the length of the string is 0 (empty string)
If [str1] is true when string str1 is non-empty

The shell uses-N to determine that the string is non-empty.

Error usage:

args=$*

If [-N $ARGS]

Then

Print "with argument"

Fi

print "Without argument"

Regardless of the pass-through parameters, will always enter if inside.

Reason: Because the IF statement is equivalent to if [-N],shell will treat it as if [str1], the-n nature is not empty, so it is positive.

Correct usage: You need to add double quotes on the $args, which is "$ARGS".

args=$*

If [-N "$ARGS"]

Then

Print "with argument"

Fi

print "Without argument"

Linux Shell if [-n] Correct use method

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.