Shell script ----- while loop and until Loop

Source: Internet
Author: User
Tags arithmetic operators

While test conditions

Do

Statement 1

Statement 2

Done

Test condition: if the condition is met, the loop is closed until the condition is not met.

How does a while loop exit? Change the variable value corresponding to the test condition in the loop body

Supplement: Arithmetic Operators

Sum = $ [$ sum + $ I] = sum + = $ I

-=

* =

Sum + = 1 = Let sum ++

Sum --

Sam = 3

While [$ Sam-le 5]

Do

Let Sam ++

Done

Example: If you enter a random number, the echo "test">/tmp/123

 #!/bin/bash     a=1     read -p "Please enter a number:"  sam     while [ $a -le $sam ]     do          echo "test" >> /tmp/123          let a++     done

Example 2: all users in the current system. If the ID number is an even number, the user name and shell are output.

#! /Bin/bashfile =/etc/passwdwhile read line read will automatically add each row in the $ file, pay the value of each row to $ linedo ID =$ (echo $ Line | cut-D:-F3) if [$ [$ id % 2]-EQ 0] $ ID in addition to the remainder of 2 equals 0, then execute then username = $ (echo "$ line" | cut-D: -F1) shell = $ (echo "$ line" | cut-D:-F7) echo-e "username: $ USERNAME \ nshell: $ Shell \ n" fidone <$ File


Column: converts the characters entered by the user to uppercase, and exits the script with the quit character.

#! /Bin/bash

Read-P "A String:" stringwhile ["$ string "! = "Quit"] If the user does not enter quit, it will loop until the string is quit do echo "$ strin" | TR "a-z" A-Z "Read-P" New string [quit for exit]: "string done

Bytes ------------------------------------------------------------------------------------

Until cyclic test conditions

Do

Statement 1

Statement 2

Done

If the condition does not meet the cycle, the loop will exit after the condition is met.

Column: converts the characters entered by the user to uppercase, and exits the script with the quit character.

#! /Bin/bash

Read-P "A String:" String

Until ["$ string" = "quit"] If the user inputs A. $ string = The quit condition is not met, the loop continues. If the $ string = quit condition is met, the system exits.

Do

Echo "$ strin" | TR "a-z" "A-Z"

Read-P "New String [quit for exit]:" String

Done

Example 2: check if the hadoop user is logged on every five seconds. the user logs on and exits. Otherwise, the current time is displayed and the hadoop user is not logged on.

#!/bin/bashuntil who | grep "^hadoop" &> /dev/null      do            echo "$(date) hadoop not login"            sleep 5donedong            echo "hadoop login"            exit 0

Example 3: The following menu is displayed to allow the user to select and output relevant information. After the user selects the menu, the corresponding information is displayed. If the user does not exit, the user is re-selected until the user enters quit to exit.

Cat <EOF
D | D) show disk Usages
M | M) show memory Usages
S | S) show swap Usages
*) Quit
EOF

---------------------------------------

#!/bin/bashcat << EOFd|D) show disk usagesm|M) show memory usagess|S) show swap usages*)quitEOFread -p "Please Input Device :" usagesuntil [ "$usages" ==  "quit" ]do      case $usages in                d|D)                           df -h                            read -p "Please Input     Device :" usages            ;;        m|M)                            free -m | head -2                            read -p "Please Input Device :" usages                            ;;                    s|S)                                free -m | tail -1                                read -p "Please Input Device :" usages                                    ;;                        *)                                echo "Input error"                                exit 7  esacdone                ;;         esacdong


This article from the "hanging sword" blog, please be sure to keep this source http://sublime.blog.51cto.com/8856101/1538989

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.