The Loop statement of Shell programming

Source: Internet
Author: User

Shell Programming Loop Statements

Learning Goals:

Mastering the FOR Loop statement programming

Mastering while looping statement programming

Directory structure:

For Loop statement

L read different variable values to execute the same set of commands individually

L For statement structure

For variable name in value list

Do

Command sequence

Done

L for execution principle

For variable = value 1,do command sequence. Take value 2, value 3 ... Take the value n, respectively do. After all the values have been executed, the done loop is completed.

L Check host status based on IP address

The IP address is stored in the Ip.txt file

One per line

Use the ping command to detect the connectivity of each host

L Check Host status script based on IP address

[Email protected] ~]# VI ip.txt

192.168.0.8 127.0.0.1

192.168.0.9

192.168.0.10

192.168.0.11

[Email protected] ~]# VI chkhost.sh

#!/bin/bash

#this is chkhosts

hlist=$ (Cat/root/ip.txt)

For IP in $hlist

Do

Ping-c 3-i 0.2-w 1 $IP &>/dev/null

If [$?-eq 0]; then

echo "Host $IP is Up"

Else

echo "Host $IP is down"

Fi

Done

[Email protected] ~]# sh chkhost.sh

Host 192.168.0.8 is up

Host 127.0.0.1 is up

Host 192.168.0.9 is down

Host 192.168.0.10 is down

Host 192.168.0.11 is down

Homemade add-on small script from 1 to 100

[Email protected] ~]# seq-s "" >>num.txt

[email protected] ~]# cat Num.txt

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 8 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

[Email protected] ~]# VI add.sh

#!/bin/bash

#description: 1 Add 2 Add 3 Add ... add 100

a=$ (Cat/root/num.txt)

C=0

For B in $a

Do

I= $b

c=$ (($i + $c))

Done

Echo $c

[Email protected] ~]# sh add.sh

5050

While Loop statement

L Repeat tests for a condition, as long as the condition is true

L While condition test operation

Do

Command sequence

Done

L While statement execution principle

While condition test operation, condition is set, do command sequence. Continue to judge the condition, set up to execute the DO command sequence. does not set the end loop on done.

L Bulk Add Users

The user starts with Stu and numbers sequentially

Add 20 users altogether, i.e. Stu1,stu2,...,stu20

The initial password is set to 123123

Instance

[Email protected] ~]# VI useadd.sh

#!/bin/bash

#一共添加20个用户, namely Stu1,stu2,...,stu20

#初始密码均设为123123

Perfix=stu

I=1

While [$i-le 20]

Do

Useradd $perfix $i

echo "123123" |passwd--stdin ${perfix}$1 &>/dev/null

i=$ (($i + 1))

Done

The Loop statement of Shell programming

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.