Extensive misunderstanding of the IF syntax structure in bash

Source: Internet
Author: User
Tags builtin
Transferred from:
Http://blog.chinaunix.net/u/8681/showart.php? Id = 1145851

I bet you are reading this article. more than 99% of the possibility is that you are wrong about this problem, or at least there is a deviation.

I believe that this is a broad mistake in the Education Law, that is, to tell everyone:
If [condition]
Then
...
Fi

This is the "Syntax" of the Condition Statement in bash ".

Anyone, or almost anyone, will naturally think that [and] are part of this syntax structure, and, honestly, in your own scripts, the IF statement is always used in this way. If you are always determining whether the string is equal or whether various attributes of the file are true, however, you may make mistakes like this: (assume that the content of $1 is-)

If ["$1" = "-a"]
Or
If ["$1 ="-a "]

After all, in many other languages, special symbols such as () and {} can be used as separators, but Bash will reply to your statement:
[-A: Command not found
Bash: [: Missing ']'

I believe that the above mental model has serious consequences of failure. The bash error message shows that it does not look at the structure of the IF Condition Statement like this internally.

The above model cannot explain the following legal bash commands:

["$1" = "-a"]

This is just a legal command. There are no such items as if, then, and Fi. Of course, the following is also legal.

["$1" = "-a"] & Echo Yes

How do you explain these bad things? Of course, you can fully complement the above if, uh, [...] this structure is so special that you must have at least one space after [and at least one space before], and [...] the structure can also appear separately, as shown above... such an explanation is a self-deception. I can assure that the people who attempt to explain this to myself are skeptical about this statement, because you don't know what else this structure will suddenly jump out and surprise you. I admit that I have experienced this idea myself.

To thoroughly criticize this false concept, let me give another example:

If PS ax | grep Oracle>/dev/null 2> & 1; then
...
Fi

First of all, this is legal. It aims to know whether there is anything related to Oracle in the process list. Of course, if you are good enough, you can see that there is another problem with this approach. but this is not my point.

What I want to talk about is how to explain the syntax model of the IF statement you have been teaching. [and] are gone, and there is a pipeline, we have seen common pipelines, but can they appear securely in the IF statement? In addition, it makes you feel familiar and secure [and] are gone.

Well, if you want to get a correct and healthy environmental protection knowledge about the if structure in bash, let's take a look:

1. first, [Bash has no special position. It is a command, just like cat, ls, grep, and so on. it is not a keyword, although its better brother "[[" Yes

Echo [
You will get [. Try again if you don't believe it. Bash will not report any errors! But this does not indicate that it is not a keyword. Echo if you can also get if

Therefore, please try again:

Which [
And
Ls-L $ (which [)

2. Then, the true if model is:

If COMMANDS; then COMMANDS; [elif COMMANDS; then COMMANDS;]... [else COMMANDS;] fi

In fact, the help if in bash gives you the correct answer, but I don't know why countless people just ignore it, many bash teaching materials use the model proposed at the beginning of this article to tell you about the if structure.

Note: ["$1" = "-a"] only has one command. [It is the command name !, "$1" is the first parameter, = is the second parameter, "-a" is the third parameter,] is 4th. it is [this command, rather than the structure like ["$1" = "-a"] in which bash is complaining, because [this command has expectations for its parameters, it expects the last parameter to be], and you can use the "-a"] concatenation form to get only three parameters: $1, = and-a].

This also explains the following errors:
["$1" = "-a"]
The bash interpreter obtains the following command line:
[-A = "-a"]
Of course, it considers [-a is the command name part, and your system does not have this command, so it will report:
[-A: command not found

3. Second, there is a builtin [command in bash. Generally, you use [This built-in command is used to execute it. The purpose is efficiency. It is too costly to start a process.

Try
Type [
Command

Almost all books will talk about it? [...] Is a special form of calling the test command. refer to the following command.
$ Help [
[: [Arg...]
This is a synonym for the "test" builtin, but the last
Argument must be a literal ']', to match the opening '['.
$ Man test
Name
Test-check file types and compare values

Synopsis
Test expression
Test

[Expression]
[]
[Option

Descriptio...

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.