Linuxshell application of script for, while, case statements

Source: Internet
Author: User
Tags case statement

First, use the FOR Loop statement

1) structure of the For statement

For variable name in value list

Do

Command sequence

Done

Execution process:

First assigns the 1th value in the list to the variable, executes the command sequence in the Do......done loop body, assigns the 2nd value in the list to the variable, and executes the sequence of commands in the loop body ... And so on, until all the values in the list have been exhausted, and the last is skipped to the done statement, which indicates the end loop.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/59/53/wKioL1TQY52ARL6jAAFi83QR-AE754.jpg "/>

2) Application Example for statement

Example 1: Bulk adding users by Name list

Create an employee list file first Users.txt

[[email protected] ~]# vim/root/users.txt//used as a test list file

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/59/57/wKiom1TQYrSBV2EfAAAaWodVITw221.jpg "/>

[[email protected] ~]# vimuaddfor.sh//Batch Add user's script

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/59/53/wKioL1TQY52wuuWUAACEG_6xkqU113.jpg "/>

[Email protected] ~]# chmod +x uaddfor.sh

[[email protected] ~]#./uaddfor.sh//test and confirm execution results

[Email protected] ~]# tail-3/etc/passwd

Chenye:x:1011:1011::/home/chenye:/bin/bash

Dengchao:x:1012:1012::/home/dengchao:/bin/bash

Zhangjie:x:1013:1013::/home/zhangjie:/bin/bash

Example 2: Deleting a user in bulk

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/59/57/wKiom1TQYrSQgMy1AABfZXJuOtg924.jpg "/>

Example 3: Checking host status based on IP address column

Create a host list file first

[[email protected] ~]# vimipadds.txt//used as a test list file

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/53/wKioL1TQY53gXPA_AAAppmxaB4o980.jpg "/>

[[email protected] ~]# vimchkhosts.sh//loop Check each host's script

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/57/wKiom1TQYrWzY9zfAADCFg4OTXY902.jpg "/>

Attention:

The IF statement, the for statement, and various other Shell script statements can be nested

Second, using the while loop statement

1) Comparison of the for statement and the while statement

The For Loop statement applies to cases where the list object is irregular and the list source is pinned (such as a list file)

The While Loop statement is intended to require a control loop count, the operand is numbered numerically, and repeated operations are performed on a specific condition

2) The structure of the while statement

While condition test action

Do

Command sequence

Done

Execution process: First judge the condition test operation result after the while, if the condition is true, execute the command sequence in the Do......done loop body, return while then judge the condition test operation result again, if the condition still holds, continue to execute the loop body; Judging condition test results ... This loop, until the condition test result after the while is no longer established, and finally jumps to the done statement, which indicates the end loop

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/59/57/wKiom1TQYrbCTS02AAFrXKSrjRQ897.jpg "/>

Attention:

1) when using a while loop statement, there are two special conditional test actions, True (true), False (False)

True: The condition is always set, and the sequence of commands within the loop will execute indefinitely unless the script is forced to terminate (or exit the script via the exit statement)

False: The loop body will not be executed

2) Use the While loop statement to avoid falling into a "dead loop"

3) While statement application example

Example 1: A user who adds a rule number in bulk

[[email protected] ~]# vimuaddwhile.sh//Batch Add user's script

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/53/wKioL1TQY5-QhFxEAACTHDSZIms858.jpg "/>

Example 2: Batch deletion of regular numbered users

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/57/wKiom1TQYrbhxlYjAABIXQ4QjTo520.jpg "/>

Example 3: Guess Price game

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/59/53/wKioL1TQY6DRba8wAAEl_uRr9dM596.jpg "/>

Third, use Case Branch statement

1) Case Statement usage

There are multiple values for a variable, and you need to execute a different sequence of commands for each of these values.

Differences from the IF statement

The IF statement needs to judge several different conditions

The case statement simply determines the different values of a variable

2) basic structure of Case statement

Case Variable value in

Mode 1)

Command sequence 1

;;

Mode 2)

Command Sequence 2

;;

......

*)

Default command sequence

Esac

Execution process:

First Use "Variable value" and the mode 1 to compare, if the same value is executed after the Mode 1 command sequence, until the double semicolon ";;" jumps to Esac, indicating that the branch ends, and if the pattern 1 does not match, then continue to compare with the pattern 2, if the same value is the same as the command sequence after mode 2, until the ";;" after jumping to ESAC, indicating the end of the branch, ... And so on, if no matching value is found, the command sequence after the default mode "*" is executed until the end of the branch is met Esac

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/59/57/wKiom1TQYrvxv9ZoAAJEvxO4q94260.jpg "/>

Attention:

1) Case line end must be the word "in", each pattern must be closed with a closing parenthesis ")"

2) Double semicolon ";;" indicates the end of the command sequence

3) in a pattern string, you can represent a contiguous range in square brackets, for example: "[0-9]"; You can also use the vertical bar symbol "|" Representation or, for example, "a| B

4) The Last "*" indicates the default mode, where * is equivalent to a wildcard character

3) Example application of Case statement

Example 1: Checking user-entered character types

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/53/wKioL1TQY6XwApvIAACwiiAQoSQ059.jpg "/>

Example 2: Writing system service scripts

[Email protected] ~]# vimmyprog.sh

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/57/wKiom1TQYrzgzpsyAAEW1WNfKbM452.jpg "/>

To administer the myprog.sh service to Chkconfig, you also need to add the appropriate identification configuration, copy the myprog.sh script to the/etc/init.d/directory, and then execute "chkconfig--add myprog.sh". Add as standard system service

650) this.width=650; "Src=" http://s3.51cto.co

Linuxshell application of script for, while, case statements

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.