Shell simple Use (b) loop

Source: Internet
Author: User

Record a few simple examples of loop-related

1. Wait for the expected content to be entered

#!/bin/Bashuntil["$yn"=="Yes"-O"$yn"=="YES" ] DoRead-P"Please input yes/yes to stop this progras:"yn DoneEcho "ok! You input the correct answer."

Wait to enter expected content before exiting

Also available while implementation

2. Multi-digit summation

While implementation

i=0s=0 while"$i"" ]do    i1))    s=$ (($s + $i))done  echo""

Implemented with for

#!/bin/bashs=0""  nu for ((i=1; I<= $nu; i=i+1  ) do-    s=$ (($s + $i))doneEcho " "

Execution results

3. String processing

A For loop handles file names individually

#!/bin/bash for on 'ls'do    echo  $var  Done

Execution results are as follows

For loop processing user information

#!/bin/bashusers=$ (cut -D:-f1/etc/passwd) for in $users do     ID  $username    finger $username Done

For loop find connectivity for specified IP address

#!/bin/bashnetwork="192.168.1" forSitenuinch$(seq  the  the) Do    Ping-C1-W 1${network}.${sitenu} &>/dev/NULL&& result=0|| result=1    if["$result"==0]; Then        Echo "Server ${network}.${sitenu} is up"    Else        Echo "Server ${network}.${sitenu} is down"    fi Done

Execution Result:

Shell simple Use (b) loop

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.