The while loop of the shell loop structure

Source: Internet
Author: User

While loop


1)


While CONDITION; Do

Statement

Statement

< change the loop condition true and false statement >

Done



Write a script that calculates 1---100 of the AND


#!/bin/bash

#


Sum=0

I=1


While [$i-le 100]; Do

Let sum= $sum + $i

Let i= $i +1

Done


Echo $sum



2)


While true; Do

Statement

Statement

Done



#!/bin/bash

#


While true; Do

Read-p "Please enter your choice:" Choice

if [$choice = = "Q"]; Then

Break

Fi

Done



#!/bin/bash

#


While true; Do

Uptime

Sleep 3

Done



3)


while read line; Do

Statement

Statement

Done < file



#!/bin/bash

#


Bash_num=0

Nologin_num=0


while read line; Do

sh_name=$ (echo $line | awk-f: ' {print $7} ')

Case $sh _name in

/bin/bash)

Let bash_num= $bash _num+1

;;

/sbin/nologin)

Let nologin_num= $nologin _num+1

;;

Esac

Done </etc/passwd


Echo $bash _num

Echo $nologin _num





Util cycle:



Util CONDITION; Do

Statement

Statement

Done


When the condition is false, the loop is executed and the condition is true, ending the loop


This article is from the "lyw666" blog, make sure to keep this source http://lyw666.blog.51cto.com/12823216/1957415

The while loop of the shell loop structure

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.