Linux while, until statements

Source: Internet
Author: User

The syntax format of a and while

Second, the grammatical format of until

Iii. Examples and Exercises

Iv. Supplementary

The syntax format of a and while

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

while test condition; Do

Statement 1

Statement 2

······

Done

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

Loop when the condition is met, and exit the loop until the condition is not satisfied

One way to exit the loop: Change the value of the corresponding variable in the loop body, as shown in Example 1 , exit the loop if the value of $I is greater than 100

Often used to traverse the contents of each line of a file, such as example 3

while read line; Do

Statement 1

Statement 2

······

Done </path/to/somefile

Second, the grammatical format of until

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

until test conditions; do

Statement 1

Statement 2

······

Done

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

The condition is not satisfied on the loop until it is satisfied, such as Example 5, until I is greater than 100, exit the loop

Iii. Examples and Exercises

Example 1: Computes the and of all positive integers within 100

#!/bin/bash#sum=0 #如果希望变量中存储的值为整数型, preferably using let sum=0 or declare-i sum=0 this form i=1while [$I-le];d o let sum+= $I #算术运算 Be careful not to omit let i++ #算术运算写成这种格式要注意不要漏掉letdoneecho "Sum: $Sum" in this format

Example 2: Calculation of all even numbers within 100 and

#!/bin/bash#evensum=0 #如果希望变量中存储的值为整数型, preferably using let evensum=0 or declare-i evensum=0 this form i=0while [$I-le 100]; Do let evensum+= $I let i++ let I++doneecho "Evensum: $EvenSum" or #!/bin/bash#evensum=0i=0while [$I-le 100]; do if [$[$I%2]-eq 0]; Then #除二取余为0时 let evensum+= $I fi let I++done

Example 3: If the user has an even ID number, the name and shell are displayed, and this is done for all users

#!/bin/bash#while read line; Do uid= ' echo $Line | cut-d:-f3 ' If [$[$Uid%2]-eq 0]; Then #除二取余为0时 echo $Line | cut-d:-f3,7 Fidone </etc/passwd #此时while语句实现了一行一行遍历整个文件内容

Example 4: Convert user input characters to uppercase, exit when input quit

#!/bin/bash#read-p "A string [Quit for quiting]:" String while ["$String"! = Quit]; Do #一定要用双引号 error when string has a space echo $String | Tr ' A-Z ' A-Z ' read-p "Next String [Quit for quiting]:" Stringdone


Example 5: Computes the and of all positive integers within 100

#!/bin/bash#sum=0i=1until [$I-GT 100]; Do #直到I的值大于100才退出 let sum+= $I let I++doneecho "Sum: $Sum"

Example 6: see if a Hadoop user is logged in every 5 seconds, sign in and sign in and exit; otherwise, the current time is displayed and Hadoop is not logged in (while shorthand)

#!/bin/bash#until who | grep "^hadoop" &>/dev/null; Do Date sleep 5doneecho "Hadoop are logged in." or #!/bin/bash#who | grep "^hadoop" &>/dev/nullret=$?until [$Ret-eq 0]; Do #grep匹配到时, the command execution status return value is 0, not matched to a non 0 date sleep 5doneecho "Hadoop is logged in." or #!/bin/bash#who | grep "^hadoop" &>/dev/nullret=$?while [$Ret-ne 0]; Do date sleep 5 who |    grep "^hadoop" &>/dev/null ret=$? #再次取得命令执行状态的返回值 Doneecho "Hadoop is logged in."

Exercise 1 : Write a script:  
1) display a menu to the user:  
d| D) show disk usages.  
m| M) show memory usages.  
s| S) show swap usages.  
*) quit.       
2)   When the user chooses to complete, displays the appropriate information, does not exit, and lets the user select again to display the content again, except that the user uses quit

#!/bin/bash# date: 2015-04-20# author: arvinlau# description: # version:  1.0cat << eof    d| D)  show disk usages.m| M)  show memory usages.s| S)  show swap usages.*)  quit. eofret=0     #自定义退出的命令执行状态返回值, through the until statement until the condition is met, otherwise cycle through the practice until [  "$Ret"  -eq  9 ];d o    read -p  "Give a choice:"  Choice     case  $Choice  in    d| D)         df -lh;;     m| M)         free -m;;     s| S)         cat /proc/swaps;;     quit)         echo  "quit."         exit 9;;    &nbSP; #自定义命令执行状态返回值     *)         echo -n  " Invalid input, ";;     esac    Ret=$?     #只要不是自定义的9, it will keep circulating done or #!/bin /bash# date: 2015-04-20# author: arvinlau# description: # version:  1.0cat << eof    d| D)  show disk usages.m| M)  show memory usages.s| S)  show swap usages.*)  quit. eofuntil [  "$Choice" &NBSP;==&NBSP;QUIT&NBSP;];d o     #当变量字符串是quit时, exiting the loop    read -p  "Give a chioce:"  choice       case   $Choice  in    d| D)         df -lh;;     m| M)         free -m;;     s| S)         cat /proc/swaps;;     quit)         echo  "quit."         exit 9;;     *)         echo -n  "invalid  Input, ";;     esacdone

Four, arithmetic operators

The operation expression for each row below is the same as the result

sum=$[$Sum + $M] Let sum+= $M

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

sum=$[$Sum +1] Let sum+=1 let sum++ # variable itself +1

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

sum=$[$Sum-$M] Let sum-= $M

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

sum=$[$Sum * $M] Let sum*= $M

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

sum=$[$Sum/$M] Let sum/= $M

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

sum=$[$Sum% $M] Let sum%= $M

This article is from the "Arvin Lau" blog, be sure to keep this source http://64314491.blog.51cto.com/2784219/1636240

Linux while, until statements

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.