Shell Script Loop statement--for loop

Source: Internet
Author: User

Learning scripts is to facilitate our operations, and for OPS, there is a lot of work that needs to be done repeatedly, such as when we manage user accounts we need to create and delete user accounts in bulk. At this time if using the system comes with the Useradd or Userdel to operate, it can only be a mechanical one of the creation, one of the deletion. Then we can consider writing a program, let it automate to create. At this point, our loop execution concept comes out. A loop is a procedure that allows a program to perform related operations repeatedly, in a certain condition, until the loop condition is met.


Common categories of circular statements:
1,for statements
2,while statements
3,until statements

The loop body consists of two parts:
1, the entry condition of the loop
2, the exit condition of the loop

Number of Cycles:
1, pre-known for statement
2, prior unknown while, until statement

Syntax format:
For variable name in list;
Loop body
Done

Loop execution mechanism: assigns the element in the list to the variable name sequentially, executes the loop body once each assignment, until the element in the list is exhausted and the loop ends

List Generation Method:
(1) Give the list directly
(2) List of integers:
(a) {start: End} such as: {1.. 10}
(b) $ (SEQ [start [step]] end) such as: ' SEQ 1 2 100 ' represents all odd numbers between 1-100
(3) Returns a list of commands (command) such as: ' Ls/etc '
(4) using glob, such as: *.sh
(5) variable reference; [email protected], $*

For loop Application Example:

1, computes the and of all positive integers between 1-100

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create TIME:2016-08-17 16:53:28
#Description: 1-100 all positive integer ' s sum

Declare-i sum=0

For I in {1..100};d o
Let sum+= $I
Done
echo "1-100 all positive integer ' sum: $sum"

2, automatic scanning 1-254 network segment of all hosts, statistics on the number of online hosts and the number of non-online hosts

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create TIME:2016-08-17 11:25:37
#Description:p ing all hosts and count the number

Declare-i online=0
Declare-i offline=0

For host in ' seq 254 ';d o
Ping-c 1-w 1 10.1.250. $host &>/dev/null && echo "10.1.250. $host is Online" && let online= $online +1 | | Let offline++
Done
echo "Online hosts number: $online"
echo "Offline hosts number: $offline"

3, generate 10 random numbers and find the maximum random number and minimum random number

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create TIME:2016-08-17 17:27:45
#Description: Random number comparative

Minrandom= $RANDOM
Maxrandom= $minrandom
Echo $minrandom

For I in {1..9};d o
rd= $RANDOM
Echo $RD
if [[$RD-gt $maxrandom]];then
Maxrandom= $RD
elif [[$RD-lt $minrandom]];then
Minrandom= $RD
Fi
Done
echo "Minrandom is: $minrandom"
echo "Maxrandom is: $maxrandom"

650) this.width=650; "title=" Forrandom.png "src=" http://s5.51cto.com/wyfs02/M01/86/2E/ Wkiom1e21soizh1oaaawsrrbiks311.png "alt=" Wkiom1e21soizh1oaaawsrrbiks311.png "/>

4, Print 99 multiplication table (first edition)

#!/bin/bash

#Author: Wangjun
#Contact qq:18353030
#Version: 1.0
#Create time:2016-08-16 13:07:40
#Description: Multiplication table

For Row in {1..9};d o
For Column in ' seq $Row ';d o
Echo-ne "${column}x${row}=$[$Row * $Column]\t"
Done
Echo
Done

5, Print 99 multiplication Table (second edition)

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create time:2016-08-18 11:35:16
#Description: Multiplication table

For ((row=1;row<=9;row++));d o
For ((column=1;column<= $row; column++));d o
Echo-ne "${column}x${row}=$[${column}*${row}]\t"
Done
Echo
Done

650) this.width=650; "title=" For99.png "src=" http://s3.51cto.com/wyfs02/M01/86/2E/ Wkiol1e21t2bdpitaabbwjest-a222.png "alt=" Wkiol1e21t2bdpitaabbwjest-a222.png "/>

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1840380

Shell Script Loop statement--for loop

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.