Use & replace if in Linux Shell

Source: Internet
Author: User

Reading the Linux Shell written by othersCodeSuddenly found a lot of code similar to the following format:

["$ Pro" = "1"] & ckssl = "-- SSL" [-n "$ IP"] & ckip = "-h $ IP" [-n" $ port "] & ckport ="-p $ port"

I didn't understand why I wrote this in the first place.

I only know that [] is equivalent to test.

After consulting a senior, I suddenly realized that I ignored the principle of execution &.

If & (and operator) is executed from left to right, the expression on the right is executed only when the execution result on the left is true. Otherwise, the expression on the right is not executed.

This format can cleverly Replace the if statement to simplify the code.

For the same purpose, if you use if:

If ["$ pro" = "1"]; then ckssl = "-- SSL" fi;

Three lines of code are required. In fact, the execution efficiency of the two methods should be the same.C LanguageThe example of a one-to-one operation in a left-side navigation pane is more efficient than the operation in step 2.

Here, you need to execute the expressions in [] and then judge the result value of the expression.

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.