Shell Loop Statement Application Example

Source: Internet
Author: User

1 for statement

Syntax format

For variable in value (or loop condition)

Do

Command

Done

Send mail to multiple users

#! /bin/bash

Domain=163.com

For user in Tom Hom Jem

Do

Mail-s "Happy New Year" [email protected] $domain </var/log/messages

Done

To print 9*9 multiplication formulas

#!/bin/bash

For i in {1..9}

Do

For ((j=1,j<=i,j++))

Do

printf "%-8s" $j * $i =$ ((j*i))

Done

Echo

Done


2 While statement


Syntax format

While condition do command done

While read-r command done < file


Add 20 users in bulk, user name usern,n to 1 to 20 digits

#!/bin/bash

U=1

While [$u-le 20]

Do

Useradd user${$u}

u=$ ((u+1))

Done

Read the print NIC profile by line

#!/bin/bash

File=/etc/sysconfig/network-scripts/ifcfg-eth0

While Read-r line

Do

Echo $line

Done < $FILE


3 until statements

Syntax format

Until condition do command done

Bulk delete users, user name Usern,n 1 to 20 digits

#!/bin/bash

U=20

Until [$u-eq 0]

Do

Userdel user${$u}

u=$ ((u-1))

Done


4 SELECT statement

Generate a Query menu with SELECT

#! /bin/bash

echo "Where is You"

Select Var in "Shenzhen" "Guangzhou" "Meiguo" "Xianggang"

Do

Break

Done

echo "You is from $var"


5 Control statements

Shift shifts the position parameter one bit to the left, that is, after shift is executed, it becomes $

Countinue interrupts the current loop and enters the next loop

Break ends the entire loop

Exit to end the run of the script




This article is from the "Practical Linux knowledge and Skills sharing" blog, please be sure to keep this source http://superleedo.blog.51cto.com/12164670/1889347

Shell Loop Statement Application Example

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.