If, case, for, while, until, shellcase in Shell scripts

Source: Internet
Author: User

If, case, for, while, until, shellcase in Shell scripts

If condition test statement

  • If-then-fi

    In the if condition test statement, the command following the if statement is run ).

    If the exit status code of the command is 0, the command in the then part will be executed.

    If the exit status code of this command is another value, the then commands will not be executed (if commands are still executed ).

    Parentheses (): In addition to the standard mathematical operators used by the test command, advanced mathematical expressions are also allowed during the comparison. The double brackets command provides more mathematical symbols. Format: (mathematical value assignment or comparison expression )).

    Parentheses

Symbol

Description

Val ++

Post-Increment

Val --

Subtraction

++ Val

Add first

-- Val

Minus

!

Logic inversion

~

Reverse search by bit

**

Power Operation

<

Move left

>

Right Shift

&

Boolean and

|

Boolean OR

&&

Logic and

|

Logic or

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

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

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

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

  • If-then-else-fi

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

    ["$ Yn" = "Y"] | ["$ yn" = "y"] (boolean logic simplifies possible return values to TRUE or FALSE)

    You can also enter ["$ yn" = "Y"-o "$ yn" = "y"]

    If condition test statement mixed shell script parameter usage

Case condition test statement

  • Case-in-esac

    The case command compares the specified variables with different modes. If the variables match the pattern, shell will execute the command specified for this pattern. You can use the | vertical line operator to separate multiple modes in one row. * The asterisk will capture all values that do not match the known mode.

For condition loop statement

  • For-do-done

    Can also be written as for var in list; do

$ Animal variable retains its value, allows us to modify its value, and is used like other variables except for the for command loop.

When the list values of the for Loop contain single quotes, shell will try to use them to define a separate data value. If you want to normally output single quotes, you must also use the Escape Character (backslash \) to escape single quotes, or use double quotation marks "" to define the value of single quotes.

The for loop assumes that each value in the list is separated by space. If a separate data value contains spaces, these values must be enclosed in double quotation marks (when a value is used on both sides, shell does not regard double quotation marks as part of the value ).

Read list from variable

Read value from command

Each row in the users.txt file has a value, so each value is not separated by spaces. The for command traverses the output of the cat command in each row.

If a value in a row contains a space character, the for command still splits the value into multiple spaces. In this case, you must change the IFS environment variable of the internal field separator (internal field separator. By default, bash shell treats spaces, tabs, and line breaks as field delimiters. That is, if any of these three characters exists in the data, bash shell assumes that it is the beginning 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 restrict the characters (IFS = $ '\ n') that the bash shell uses as the field separator ').

IFS in SHEll:

IFS in Shell:

What should I do with a file directory with spaces? : Http://bbs.chinaunix.net/thread-4141351-1-1.html>

If the default IFS value is used, "dayo y" is split into "dayo" and "y" for data processing.

Assume that you want to traverse the values separated by colons in a file (for example, in the/etc/passwd file), you need to set the IFS value to colons.

IFS =:

If you want to specify multiple IFS characters, you only need to concatenate them in the value assignment line.

IFS = $ '\ N ':;"

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

IFS = $ '\ n', IFS = "\ n" or IFS =' \ n' differences:

The exact meaning of IFS = $ '\ N':

  • C-language for loop commands (numerical processing)

Shell loop variable transfer problem:

Scope of variables in shell loops:

While condition loop statement

  • While-do-done

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

Until condition loop statement

  • Until-do-done: the loop is terminated only when the condition is set. Otherwise, the loop continues. That is, bash shell executes the commands listed in the loop only when the exit status code of the test command is not 0. Once the test command returns the exit status code 0, the loop ends.

    In the until loop, you can also use multiple test commands. The rules are similar to those in the while LOOP, that is, only when the last command is set to stop.

Example exercise:

  • Nested loop, modify IFS environment variables, and output processing: process data in the/etc/passwd file cyclically and output them to the usersoutput.txt file (pipeline command processing is also supported)

  • Specify the format to obtain the test conditions from the file: create multiple users, modify the format of users.csv in the input file, use the read command to obtain the variable, and use the useradd command to add new users.

Control Loop break and continue commands (applicable to for, while, and until loops. Its usage and function are similar to those in C language and are not demonstrated)

  • Break n: n indicates the loop level to jump out. By default, n is 1, indicating that the current loop exists. If n is set to 2, the break command jumps out of the next level of External Loop.
  • Continue n: n indicates the loop level to be aborted. By default, n is 1, indicating that the current loop is aborted. If n is set to 2, The continue command terminates the next level of External Loop.

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.