Scripting for special usage of Bash programming loop statements under Linux (10)

Source: Internet
Author: User

Scripting for the special use of the while statement under the Linux Bash Program (10)

1. Loop control:

Break: Interrupts the entire loop statement, which is the statement following the execution of the script after exiting the loop

Continue: Interrupt the current cycle, advance to the next round cycle

Exit: End Script Run

2.while dead Loop, that is, when you do not know how many times the loop

Format: while:; Do

Looping statements

Done

3.while reads and assigns a value to the read variable from each line in the input redirection file

Format: while read Line;do

Looping statements

Done < path files

4. Instance Scripts

4.1. Find the user login in the/etc/passwd file is bash,uid greater than 500 of the top 3 users, and display their user name and UID

#!/bin/bash

Let I=1

While read line; Do

If [' Echo $LINE | cut-d:-f3 '-lt 500]; Then determine if the UID is less than 500

Continue

Fi

[' Echo $LINE | awk-f: ' {print $7} ' = = '/bin/bash '] && echo $LINE | Awk-f: ' {print $1$3} ' && let ++i

[$I-gt 3] && break

Done < /etc/passwd

~

4.2. Check if the willow user is logged into the system, unless you enter Q or Q to exit the script, otherwise enter the dead loop

#!/bin/bash

While :;d o

Read-p "q|  Q is Exit,other show does Willow come this system? "CHOICE

if [$CHOICE = = "Q"-o $CHOICE = = "Q"]; Then

echo "quiting this script"

Exit 3

Fi

If who | grep Willow &>/dev/null; Then

echo "Willow is loginnig the system at ' date '"

Else

echo "Willow is not comming at ' date '"

Fi

Done


This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1767492

Scripting for special usage of Bash programming loop statements under Linux (10)

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.