Fun Bash Script: loop structure-for Loop

Source: Internet
Author: User

Fun Bash Script: loop structure-for Loop
For Loop
Basic Format

For variable in Value List do various operations done
There is also a rare way to write a line:
For variable in Value List; do operations; done
The Value List can be roughly divided into enumeration and iteration types.
Enumerative normal enumerated Value List is a string separated by space or carriage return.
for i in 'apple' 'meat' 'sleep' 'woman'do    echo I like $idone
Run the script for. sh on the terminal. Running result
jelly@X:~$ bash for.sh I like appleI like meatI like sleepI like woman
Replace with the command replacement command, that is, the use of the ''and $ () operators. It is common to replace commands with for loops.
For example, my system user is jelly. Now I have created a user named guodong. However, the guodong user lacks many group permissions. I want guodong to have all groups where jelly is located.
So I can:
For var in 'groups jel' do echo $ var # print the group name gpasswd-a guodong $ vardone
Run the script as root to complete a batch add group task for the user guodong.
Iterative curly braces {} digital iterations, such as {1 .. 100}
Letter iteration, such as {a. z}, {A. Z}, {Z.}
ASCII character iteration, such as {a ..}

To calculate the sum of 1 to 100

#!/bin/bashans=0for i in {1..100}do    let ans+=$idoneecho $ans

Result 5050.

You can also specify the increment for the iteration of curly braces in the following format:

{First... end... increment}

Calculate the sum of all the odd numbers between 1 and 100:

for i in {1..100..2}do    echo $idone
Seq

It must be replaced with a command. Seq command format:

Seq first number [increment] Last number

Note that the increment position is in the middle, which is different from the curly braces mentioned above.

Let's look at an example (adapted from Shell Scripting Expert Recipes for Linux, Bash, and More P114)

Use the script to ping the host in the LAN:

#!/bin/bashPREFIX=192.168.1.for i in `seq 100 110`do    echo -n "${PREFIX}$i "    ping -c5  ${PREFIX}${i} >/dev/null 2>&1    if [ "$?" -eq 0 ];then        echo "OK"    else        echo "Failed"    fidone

Of course, for loop can also write for I in {100 .. 110}

Terminal running result

jelly@X:~$ bash ping.sh 192.168.1.100 Failed192.168.1.101 Failed192.168.1.102 OK192.168.1.103 OK192.168.1.104 OK192.168.1.105 OK192.168.1.106 Failed192.168.1.107 Failed192.168.1.108 Failed192.168.1.109 Failed192.168.1.110 Failed
C-style for Loop

Bash also supports a C-language for loop. This is easy to understand. Let's take a look at the example and calculate the sum of 1 to 100.

#! /Bin/bashans = 0for (I = 1; I <= 100; I ++) do let ans + = $ idoneecho $ ans note !!! The for loop must contain two parentheses.

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.