Shell foot This report wrong: "[: =: unary operator expected"

Source: Internet
Author: User

Shell foot This report wrong: "[: =: unary operator expected"

When matching strings are equal, I use a statement like this:

if [$STATUS = = "OK"]; Then

echo "OK"

Fi

At run time appeared [: =: unary operator expected error, has been unable to find the reason, tried to delete the space on both sides of the equal sign and the space in parentheses did not work, finally Baidu a bit, to find the reason. Change the sentence so that it doesn't go wrong.

if [[$STATUS = "OK"]];

Then

echo "OK"

Fi

The reason is because if the variable status value is empty, then it becomes [= "OK"], obviously [and "OK" is not equal and missing the [symbol, so reported this error. Of course not always error, if the variable status value is not empty, the program is normal, so this error is still very covert.

Or you can avoid this error by using the following method: if ["$STATUS" x = = "OK" x]; Then Echo

"OK" fi. Of course, X can also be other characters. Incidentally, there are no double quotes in the shell that are consistent in many cases.

Transferred from: http://hi.baidu.com/vishare/blog/item/bd8ab9ee289753252cf53417.html

Shell foot This report wrong: "[: =: unary operator expected"

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.