If, case, for, while, until in shell scripts

Source: Internet
Author: User

If condition test statement

    • If-then-fi

      The IF condition test statement runs the command after the if (judgment).

      If the exit status code for this command is 0, the command in the then section will be executed.

      If the exit status code for the command is a different value, then the command in the then section will not be executed (if the command is still executed).

      Double brackets (()): In addition to the standard mathematical operators used by the test command, an advanced mathematical expression is also allowed in the comparison process, and the double-brace command provides more mathematical notation. Format: ((mathematical assignment or comparison expression)).

      Double parenthesis Command symbol

Symbol

Describe

val++

val--

minus

++val

first increment

--val

first minus

!

logical negation

~

bitwise negation

**

<<

move left

>>

move right

&

bit Boolean and

|

bit Boolean or

&&

Logic and

||

Logical OR

The role of various parentheses in the shell (), (()), [], [[]], {}:

The difference between $ (()) and $ (), ${} in the shell:

The difference between $ (()), $ (), and ' ${} in the shell:

double brackets [[]]: Provides advanced features (pattern matching) for string comparisons. In pattern matching, you can define a regular expression to match a string value.

    • If-then-else-fi

    • If-then-elif--then-else-fi

      ["$yn" = = "Y"] | | ["$yn" = = "Y"] (Boolean logic simplifies the possible return values to TRUE or FALSE type)

      can also be written ["$yn" = = "Y"-o "$yn" = = "Y"]

      Use of the IF condition test statement mixed Shell script parameter

Case Condition Test Statement

    • Case-in-esac

      The case command compares the specified variable to a different pattern. If the variable and pattern are matched, the shell executes the command specified for the pattern. multiple patterns can be separated by the | Vertical line Operation Noon . * asterisks capture all values that do not match the known pattern.

For Condition Loop statement

    • For-do-done

      can also be written for the Var in list;

    1. In the first cycle, the content of the $var is con1;
    2. In the second cycle, the content of the $var is con2;
    3. In the third cycle, the content of the $var is Con3;
    4. ......

$animal variable retains its value, it also allows us to modify its value and use it in the same way as other variables outside of the for Command loop.

when single quotes appear in the list values for the For loop , the shell tries to use them to define a separate data value. If you want the normal output single quotation mark ', you must use an additional escape character (backslash \) to escape the single quotation mark, or use the double quotation mark "" to define the value used in single quotation marks.

The for loop assumes that each value in the list is separated by a space. If there are spaces in a separate data value, you must enclose the values in double quotation marks (the shell does not use double quotes as part of the value when using double quotes around a value).

Reading a list from a variable

Reading a value from a command

Each row in the Users.txt file has a value, so each value is not separated by a space. The for command iterates through the output of the cat command one line at a time.

If the value in a row has a space character, the for command will still divide the value by a space into multiple. The internal field delimiter (internal field separator) IFS environment variable must be changed at this time. By default, the bash shell treats spaces, tabs, line breaks as field separators, and the even if data has a any of these three characters, which the bash shell assumes as the start of a new data field. To solve this problem, you can temporarily change the value of the IFS environment variable in the shell script to limit the character (ifs=$ ' \ n ') that the bash shell treats as the field delimiter.

IFS in the shell:

IFS doubts in the shell:

How does a file directory with spaces work? :

If you follow the default IFS values, "Dayo y" is divided into "Dayo" and "Y" two data processing.

Suppose you want to traverse a colon-delimited value in a file (for example, in a/etc/passwd file), you need to set the value of the IFS to a colon

Ifs=:

If you want to specify more than one IFS character, simply string them up in the line of the assignment.

ifs=$ ' \ n ':;"

This assignment will use line breaks, colons, semicolons, and double quotation marks as field separators.

ifs=$ ' \ n ', ifs= ' \ n ' or ifs= ' \ n ' difference:

The exact meaning of ifs=$ ' \ n ':

    • C language-style for loop command (numeric processing)

    1. Initial value: The initial value of a variable in the loop;
    2. Limit value: When the value of the variable is within the range of the limit value, the loop continues;
    3. Execution step: The amount of variation in the variable each time the loop is made.

Shell loop variable delivery problem:

Scope of variables in the shell loop:

While conditional loop statement

    • While-do-done

      You can define multiple test commands on a while statement line, and each test command must appear on a separate line. But only the exit status code of the last Test command is used to determine when to end the loop . In a while statement with multiple commands, all test commands are executed in each iteration, including the last iteration of the test command failure.

Until conditional loop statement

    • Until-do-done: When the condition condition is established, the loop is terminated, or the program segment is continuously cycled. That is, only the exit status code of the test command is not 0,bash Shell executes the commands listed in the loop, and once the test command returns exit status code 0, the loop ends.

      Multiple test commands can also be used in the until loop, which is similar to the while loop, where the last command is set to stop.

Example exercises:

    • nesting loops, modifying IFS environment variables, and processing of outputs: looping /etc/passwd file data and redirecting output to the Usersoutput.txt file (also supports pipeline command processing)

    • Specify the format to get test conditions from a file: Create multiple users, modify IFS according to the format of the input file Users.csv, get the variable by the Read command, add a new user with the Useradd command

Control loop break, continue command (for a, while, until loop, usage and action similar to C language, no demo)

    • Break N: # Specifies the loop level to jump out. By default, n is 1, which indicates that the current loop is jumping out. Setting n as the 2,break command jumps out of the next level of external loops.
    • Continue N: = Specifies the loop level to abort. By default, n is 1, indicating that the current loop is aborted. Setting n as the 2,continue command aborts the next level of external loops.

If, case, for, while, until in shell scripts

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.