Linux command: While loop

Source: Internet
Author: User

while Introduction to Commands:

While loop: For scenarios where the number of cycles is unknown, there is an exit condition, otherwise it becomes a dead loop.

1 . Command format:

Grammar:

While CONDITION; Do

Statement

...

Done

2 . Command function:

While loop: For scenarios where the number of cycles is unknown, there is an exit condition, otherwise it becomes a dead loop.

3 . Command parameters:

While CONDITION; Do condition: conditions for entering the loop

Statement

...

Done

4 . Command instance:

1, calculate the integer within 100 and

[Email protected] test]# vim while100.sh

#!/bin/bash

#program:

#练习使用while循环计算1到100正整数的和

#history Donggen 2016-11-02-21:21

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Declare-i I=1

Declare-i sum=0


While [$I-le 100]; Do

Let sum+= $I

Let i++

Done

echo "$SUM"

[Email protected] test]# chmod +x while100.sh

[Email protected] test]#./while100.sh

5050 positive integers from 1 to 100 and 5050

[Email protected] test]#


2, if the user is prompted to enter characters, if not quit the lowercase letters all into uppercase,

Quit the script if it is quit.

[Email protected] test]# vim whilequit.sh

#!/bin/bash

#program:

#如果输入的不是quit则把小写字母全部换成大写字母, quit quit the loop

#history Donggen 2016-11-02-21:31

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Read-p "Input Something:" SCRIPT


while [$SCRIPT! = "Quit"]; Do

echo "$SCRIPT" | Tr ' A-Z ' A-Z

Read-p "Input Something:" SCRIPT

Done

[Email protected] test]# chmod +x whilequit.sh

[Email protected] test]#./whilequit.sh

Input SOMETHING:ABC

Abc

Input SOMETHING:EROQE

Eroqe

Input Something:quit

[Email protected] test]#


3, write a script to see if a user is logged in, if not login to continue to view, if the login

User has logged in and exited the loop.

[Email protected] test]# vim whiledonggen.sh

#!/bin/bash

#program:

#查看用户是否登录

#history Donggen 2016-11-02-21:45

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Usernum=donggen


W.H.O. | grep "$USERNUM" &>/dev/null

Num=$?

While [$NUM-ne 0]; Do

echo "$USERNUM is not login."

Sleep 3

W.H.O. | grep "$USERNUM" &>/dev/null

Num=$?

Done

echo "The $USERNUM is login."

[Email protected] test]# chmod +x whiledonggen.sh

[Email protected] test]#./whiledonggen.sh

Donggen is not login. Donggen users are not logged on without always prompting for no sign-in

Donggen is not login.

Donggen is not login.

Donggen is not login.

The Donggen is login. Donggen the user to log in and exit the loop after logging in.

[Email protected] test]#


This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1868711

Linux command: While loop

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.