The difference and usage of while and for in shell programming _linux shell

Source: Internet
Author: User
Tags sleep ssh

Loops are often used in shell programming, and the commonly used loops are for and while loops. While loops read files by default in rows, and for loops read file-splitting files with spaces, this article combines some examples of current web usage and differences.

First, common grammar

1, for Loop

The syntax structure used for a For loop is as follows:

For variable in SEQ string
For variable in ' command '
For variable in "$@" or "$*"
for ((assignment; condition; Operation statement)

2, while loop

The following syntax structures are commonly used in the while loop:

While [$i-lt num] While
true while
read a b C; does command done < filename
cat filename | while read a b C

Second, row read sample

For example, the common DF disk information is used to understand the difference between using a for and a while to handle several loop methods. Take a look at the script I wrote, which reads as follows:

#/bin/bash
# AUTHOR:YANGBK
# site:www.361way.com
# mail:itybku@139.com
# # Desc:test loop for in and While
Df-hl|awk ' int ($) >30 ' > Testfile
result= ' Df-hl|awk ' int ($) >30 '
Echo ' **************** For testing ***************** ' to
i in $result;d o
echo $i
do
echo ' ******************* while echo Test ************* '
echo $result | While the read line does echo
$line done
Echo ' ************* While testing **************** '
df-hl|awk ' int ($) >30 ' |while read line
do
echo $IP ' hostname ' $ Line
doing
Echo ' ****************** while read file ************** ' while read line do
echo $ IP ' hostname ' $line done
< testfile

The above script executes with the following results:

# sh forwhile.sh
******************* for testing *****************
/dev/sda3
9.5G
5.7G
3.4G
64%
/
/dev/sda2
39G
19G
18G
52%
/home
/dev/sda6
9.5G
7.1G
2.0G
78%/
usr
******************* while echo test *************
/dev/sda3 9.5G 5.7G 3.4G 64% //dev/sda2 39G 19G 18G 52%/home/dev/sda6 9.5G 7.1G 2.0G 78%/usr
****************** while testing ****************
Localhost/dev/sda3 9.5G 5.7G 3.4G 64%/
localhost/dev/sda2 39G 19G 18G 52%/home
localhost/dev/sda6 9.5 G 7.1G 2.0G 78%/usr
****************** while read file **************
localhost/dev/sda3 9.5G 5.7G 3.4G 64%/< C28/>localhost/dev/sda2 39G 19G 18G 52%/home
localhost/dev/sda6 9.5G 7.1G 2.0G 78%/usr

As you can see, only the next two methods can get the data we want, and the first two methods are different from the ones we want. The result of this example is:

1, while loop: Read the file in rows, the default separator is a space or tab;

2, for loop: Read the file with a space, that is, hit the space, began to execute the loop body, so you need to read the words, the space will be converted to other characters.

Third, SSH connection and wait

Here's an example of a test script:

#!/bin/bash
# AUTHOR:YANGBK
# # site:www.361way.com
# mail:itybku@139.com
# # Desc:test wait and SSH W Hen use with while # while
loop
echo-en "\ t";d ate
cat abc.txt|while read User IP do
{
ssh -O connecttimeout=10 $user @ $IP "hostname" </dev/null sleep
10s
} & do wait
echo " This is the
while loop. Echo-en "\ t";d ate sleep
10s
echo-e "\ n"
# for loop
echo-en "\ T";d ate for line in
' Cat abc.txt| Sed-e ' s//--/g ' do
{
user= ' echo $line |awk-f '--' {print '} '
ip= ' echo $line |awk-f '--' {print $} '
ssh-oconnecttimeout=10 $user @ $IP "hostname" Sleep
10s
} & do wait
echo " This is the
for loop. Echo-en "T";d ate

The result of this example is no longer output, you can use the script ssh several hosts to do a test, the test results are as follows:

1. For loop: The loop body executes in the background, waits for the loop body to complete execution, and then executes the following command.

2, while loop: Wait does not play a role, the loop body in the background to execute, the following command is also in execution. When there is SSH, SCP and Sshpass in the circulation system, there are two ways to solve the problem, such as:

A, the use of ssh-n "command";
b, add a null redirect within the while loop, such as SSH "cmd" </dev/null to redirect input into SSH.

Iv. efficiency of implementation

The test while is faster when you are reading a large file by row (for the I in ' cat filename ' can be read in rows) when reading a file.

Shell:for And while usage

Writing a:

----------------------------------------------------------------------------
#!/bin/bash

While Read line
Todo
Echo $line
Done < file (files to be read)
----------------------------------------------------------------------------

Script Two: ( concurrency scripts are used sparingly, grep cannot output all matching information)
----------------------------------------------------------------------------
#!/bin/bash

Cat file (files to be read) | While Read line
Todo
Echo $line
Done
----------------------------------------------------------------------------

Writing three:

----------------------------------------------------------------------------
For line in ' cat file (files to be read) '
Todo
Echo $line
Done
----------------------------------------------------------------------------

Description

For row-by-line reading and while-line-by-row reading are different, such as:

$ cat File
aaaa
bbbb FFF ggg
cccc dddd
$ cat File | While read line; do echo $line, done
aaaa
bbbb FFF GGG
CCCC dddd $ for line in
$ (<file), do echo $line, done
aaaa
bbbb
FFF
GGG
dddd

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.