Conditional control in shell scripts and the use of loops

Source: Internet
Author: User
Tags echo command

Reprint please indicate: http://www.cnblogs.com/winifred-tang94/

If conditional statement syntax:

If [conditional expression]

Then

Code

Else

Code

Fi

Note : In the IF condition statement above, there should be a space between if and [, and the conditional expression and [] on both sides should have spaces, and the conditional expression cannot be next to [and].

An IF condition expression ends with fi.

Eg.

As a result, the following issues occur:

The reason is because there is a/after the beginning of the #!, plus/After the interpreter error is not, the code is as follows:

However, there are still no errors for that file or directory. For this error I think echo after the expression has a problem, so write-off the expression, change output small and bigger, the result program runs correctly, output small or bigger.

So for the echo expression, I put the expression in double quotes, the result runs successfully, no problem.

Conditional judgments are made using case expressions:

Case variable in

STR1)

command string;;

STR2)

command string;;

*)

Eg.

The error has just occurred because the code does not end correctly, and the case condition statement should end in Esca in the shell.

The error appears as follows:

Reason:

* later) for the Chinese symbol, change it to an English symbol, and the end of the case condition statement should be ESCA instead of EASC, corrected as follows:

Right, the correct result is as follows:

Using a For loop in a shell script

There are three types of loops that can be used in the shell:

A, for Loop

The first form of syntax:
for Var in list

Do

Command string

Done

Note that the list above can be separated by a space of $1, or a space-delimited value of 2 3.

The second form of syntax:

for Var

Do

Command string

Done

In this form, the list is omitted, then the list refers to the command line parameters, each cycle of Var is a command-line parameter.

eg. output command-line arguments and

Note that the above shell program in the echo command parameters-E is for \c and \b to take effect, where \c is the output addend after the line is not wrapped, \b is the meaning of the backspace delete the extra + number.

An error occurred after running the script:

Reason:

Expr can not drink test instruction-gt after marriage together, otherwise will expr change to test, otherwise will-GT changed to >=

, it is worth noting that >= is used in front of \.

The error remains:

The reasons are:

When sum is summed, there must be no spaces at both ends .

Switch

After running the result is:

B, while loop

Syntax format:
While-conditional expression

Do

command line string;

Done

eg. output command line each parameter

The shift command shifts the command-line arguments to the left, which is the first parameter in the second position and then the output.

The result is:

Note that the conditional expression for the while loop also separates the conditional expression from [and] with a space.

If you want to use another variable n in the while, represent the unary expression used in the loop body in the form of-N:

Of course, if you do not add-N in the while, there is the same result:

C, until cycle

The syntax format is as follows:

Until conditional expression

Do

command string;

Done

Conditional control in shell scripts and the use of loops

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.