Shell script Common script: For loop

Source: Internet
Author: User

 Shellscript Common script:ForLoops

Wheil Many loops can be replaced with a for loop

For loop syntax structure

For variable name in variable Value list

Do

instruction

Done

For ((EXP1;EXP2;EXP3))

Do

instruction

Done

Script instance:For 99multiplication Table

#!/bin/bash

#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"

#Author: Jorbabe

#Mail: [Email protected]

#Function: The multiplication table

#Version: version V1.1

#Update: 2016-11-22 15:04:12

For a in ' SEQ 1 9 '

Do

For b in ' SEQ 1 9 '

Do

If [$a-ge $b];then

Echo-en "\t$a x $b" = $ (expr $a \* $b)

Fi

Done

echo ""

Done

[Email protected] for]#./for02.sh

1 x 1 = 1

2 x 1 = 2 2 x 2 = 4

3 x 1 = 3 3 x 2 = 6 3 x 3 = 9

4 x 1 = 4 4 x 2 = 8 4 x 3 = 4 x 4 = 16

5 x 1 = 5 5 x 2 = 5 x 3 = 5 x 4 = 5 x 5 = 25

6 x 1 = 6 6 x 2 = 6 x 3 = 6 x 4 = 6 x 5 = 6 x 6 = 36

7 x 1 = 7 7 x 2 = 7 x 3 = 7 x 4 = 7 x 5 = 7 x 6 = 7 x 7 = 49

8 x 1 = 8 8 x 2 = 8 x 3 = 8 x 4 = 8 x 5 = 8 x 6 = 8 x 7 = 8 x 8 =

9 x 1 = 9 9 x 2 = 9 x 3 = 9 x 4 = 9 x 5 = 9 x 6 = 9 x 7 = 9 x 8 = 72 9 x 9 = 81

  Script instance:For 1-100sum

#!/bin/bash

#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"

#Author: Jorbabe

#Mail: [Email protected]

#Function: 1-100 sum

#Version: version V1.1

#Update: 2016-11-22 15:04:12

for ((i=0; i<=100; i++)

Do

((J=j+i))

Done

#echo $j

[-N "$j"] && printf "totalsum is: $j \ n"

[Email protected] for]#./for03.sh

Totalsum is:5050

Script instance: Batch file creation

#!/bin/bash

#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"

#Author: Jorbabe

#Mail: [Email protected]

#Function: Batch file Creation

#Version: version V1.1

#Update: 2016-11-22 15:04:12

# recursively create files and go to directory

Mkdir-p/tmp/shell/for/test/&& Cd/tmp/shell/for/test

For FileNum in ' seq 10 '

Do

Touch jorbabe-$filenum

Done

[email protected] for]# ll test/

Total 0

-rw-r--r--. 1 root root 0 Nov 10:36 jorbabe-1

-rw-r--r--. 1 root root 0 Nov 10:36 jorbabe-10

-rw-r--r--. 1 root root 0 Nov 10:36 jorbabe-2

.............

 Script instance: Bulk create user settings password

#!/bin/bash

#Date: 2016-11-22 15:04:12 # #date "+%y-%m-%d%h:%m:%s"

#Author: Jorbabe

#Mail: [Email protected]

#Function: Batch Create user and set random password

#Version: version V1.1

#Update: 2016-11-22 15:04:12

# Call System library

. /etc/init.d/functions

#>/tmp/shell/for/test/user.log

#>/tmp/shell/for/test/fail_user.log

# Value 01-10

For N in $ (seq-w 10)

Do

# Time Format

tim= ' Date ' +%y_%m_%d%h:%m:%s "'

# Random Password

Passwd= ' echo $ (date +%t%n) $RANDOM |md5sum|cut-c 2-9 '

# Create user account

Useradd jorbabe-$n >&/dev/null && user_status=$?

# Set User password

echo "$passwd" |passwd--stdin jorbabe-$n >&/dev/null && pass_status=$?

# Check user created in Settings

If [$user _status-eq 0-a $pass _status-eq 0]; Then

Action "AddUser jorbabe-$n"/bin/true

# Create success, generate account information write to log

Echo-e "$tim \tuser:jorbabe-$n \tpass: $passwd" >>/tmp/shell/for/test/user.log

Else

Action "AddUser jorbabe-$n"/bin/false

# Create failed, generate account information write to log

Echo-e "$tim \tuser:\tjorbabe-$n pass:\t$passwd" >>/tmp/shell/for/test/fail_user.log

Fi

Done

[Email protected] for]#./for05.sh

AddUser jorbabe-01 [OK]

AddUser jorbabe-02 [OK]

AddUser jorbabe-03 [OK]

AddUser jorbabe-04 [OK]

AddUser jorbabe-05 [OK]

AddUser jorbabe-06 [OK]

AddUser jorbabe-07 [OK]

AddUser jorbabe-08 [OK]

AddUser jorbabe-09 [OK]

AddUser jorbabe-10 [OK]

[email protected] for]# cat Test/user.log

2016_11_22 12:08:43 user:jorbabe-01 pass:84327849

2016_11_22 12:08:43 user:jorbabe-02 Pass:cf11a1ee

2016_11_22 12:08:43 user:jorbabe-03 pass:2a60233d

2016_11_22 12:08:43 user:jorbabe-04 pass:2d181418

2016_11_22 12:08:43 user:jorbabe-05 Pass:641f69d6

2016_11_22 12:08:43 user:jorbabe-06 pass:b8d57d2a

2016_11_22 12:08:43 user:jorbabe-07 PASS:AE123CB3

2016_11_22 12:08:44 user:jorbabe-08 pass:db1f73c1

2016_11_22 12:08:44 user:jorbabe-09 pass:a6bc81e1

2016_11_22 12:08:44 user:jorbabe-10 Pass:22aac1f2

[Email protected] for]#

Delete the user account created by the script

[[email protected] for]# for N in $ (seq-w);d o userdel-r jorbabe-$n;d One


Shell script Common script: 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.