Shell script Syntax -- if/then/elif/else/fi

Source: Internet
Author: User


Shell script Syntax -- if/then/elif/else/fi is similar to the C language. In Shell, use commands such as if, then, elif, else, And fi to implement branch control. This process control statement is essentially composed of several Shell commands, such as www.2cto.com if [-f ~ /. Bashrc]; then .~ /. Bashrcfi is actually three commands, if [-f ~ /. Bashrc] is the first, then .~ /. Bashrc is the second and fi is the third. If the two commands are written in the same line, they must be separated by a; number. If only one command is written in one line, no; number is required. In addition, there is a line break after then, but this command has not been written, shell will automatically continue the line and process the next line following then as a command. Like [commands, note that commands and parameters must be separated by spaces. The parameters of the if command form a sub-command. if the Exit Status of the sub-command is 0 (true), run the sub-command after then, if Exit Status is not 0 (false), run the subcommand after elif, else, or fi. The sub-commands after if are usually test commands, but can also be other commands. The Shell script does not have {} brackets, so fi is used to indicate the end of the if statement block. See the following example :#! /Bin/sh if [-f/bin/bash] then echo "/bin/bash is a file" else echo "/bin/bash is NOT a file" fiif :; then echo "always true"; fi: A Special Command called an empty command. This command does not do anything, but the Exit Status is always true. In addition, you can run/bin/true or/bin/false to obtain the true or false Exit Status. Let's look at another example :#! /Bin/sh echo "Is it morning? Please answer yes or no. "read YES_OR_NOif [" $ YES_OR_NO "=" yes "]; then echo" Good morning! "Elif [" $ YES_OR_NO "=" no "]; then echo" Good afternoon! "Else echo" Sorry, $ YES_OR_NO not recognized. enter yes or no. "exit 1 fiexit 0 the read command in the above example is used to wait for the user to enter a string and store it in a Shell variable. In addition, Shell also provides the & | syntax, which is similar to the C language and has the Short-circuit feature. Many Shell scripts like to write like this: test "$ (whoami )"! = 'Root' & (echo you are using a non-privileged account; exit 1) & equivalent to "if... then... ", and | equivalent to" if not... then... ". & | Used to connect two commands. The-a and-o mentioned above are only used to connect two test conditions in the test expression. Pay attention to their differences. For example, test "$ VAR"-gt 1-a "$ VAR"-lt 3 is equivalent to test "$ VAR"-gt 1 & test "$ VAR"-lt 3

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.