In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about the differences between while and for in Shell programming. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. Common grammar
1. For cycle
The common grammatical structures of for loops are as follows:
For variable in seq string
For variable in `command` "
For variable in "$@" or "$*"
For ((assignment; condition; Operation statement))
2. While cycle
The common grammatical structures of while loops are as follows:
While [$I-lt num] while truewhile read a b c; do command done
< filenamecat filename | while read a b c 二、行读取示例 这里以常见的df获取磁盘信息为例,了解下使用for和while的几种循环方法处理时的区别。先看下我写的脚本,内容如下: #/bin/bash## author: yangbk## site: www.361way.com## mail: itybku@139.com## desc: test loop for in and whiledf -hl|awk 'int($5) >30'> testfileresult= `df-hl | awk 'int ($5) > 30' `echo'* for testing * 'for i in $result Doecho $idoneecho'* while echo test * 'echo $result | while read linedoecho $linedoneecho' * while testing *'df-hl | awk 'int ($5) > 30' | while read linedoecho $IP `hostname` $linedoneecho'* while Read file * 'while read linedoecho $IP `hostname` $linedone < testfile
The result of the above script execution is as follows:
# sh forwhile.sh* for testing * / dev/sda39.5G5.7G3.4G64%//dev/sda239G19G18G52%/home/dev/sda69.5G7.1G2.0G78%/usr* while echo test * / dev/sda39.5G5.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% / homelocalhost / dev/sda6 9.5G 7.1G 2.0G 78% / usr* while read file * localhost / dev/sda3 9.5G 5.7G 3.4G 64% / localhost / dev/sda2 39G 19G 18G 52% / homelocalhost / dev/sda6 9.5G 7.1G 2.0G 78% / usr
As you can see, only the latter two methods can get the data we want normally, and the first two methods are different from the results we want when dealing with them. The result of this example is:
1. While loop: reads the file by line. The default delimiter is space or Tab.
2, for loop: read the file with spaces, that is, when you encounter spaces, you begin to execute the loop body, so if you need to read in lines, convert the spaces into other characters.
III. Ssh connection and wait
Let's take a test script as an example:
#! / bin/bash## author: yangbk## site: www.361way.com## mail: itybku@139.com## desc: test wait and ssh when use for in and while# while loopecho-en "\ t"; datecat abc.txt | while read user ipdo {ssh-o ConnectTimeout=10$ user@$ip "hostname" < / dev/nullsleep 10s} & donewaitecho "This is while loop." echo-en "\ t"; datesleep 10secho-e "\ n" # for loopecho-en "\ t" Datefor line in `cat abc.txt | sed-e's / /-/ g '`do {user= `echo $line | awk-F'-'{print $1} '`ip= `echo $line | awk-F' -'{print $2} '`ssh-oConnectTimeout=10$ user@$ip "hostname" sleep 10s} & donewaitecho "This is for loop." echo-en "\ t"; date
The result of this example is no longer output here. You can use this script to ssh several hosts to do a test. The test results are as follows:
1. For loop: the loop body executes in the background, waiting for the loop body to finish, and then the following commands are executed.
2. While loop: wait does not work, the loop body is executed in the background, and the following commands are also executed at the same time. When there are ssh, scp and sshpass in the loop, there are two ways to solve this problem:
A. Use ssh-n "command"
B. Add null redirection to the while loop, for example, ssh "cmd" < / dev/null redirects the input of ssh.
IV. Implementation efficiency
In terms of the efficiency of reading large files by line (when for reads files, for i in `cat filename` can read by line), while is faster after testing.
Shell:for and while usage
Write method 1:
#! / bin/bash
While read line
Do
Echo $line
Done < file (file to be read)
Writing method 2: (concurrent scripts are used cautiously, grep cannot output all matching information)
#! / bin/bash
Cat file (File to be read) | while read line
Do
Echo $line
Done
The third way of writing:
For line in `cat file (file to be read) `
Do
Echo $line
Done
Description:
There is a difference between for line-by-line reading and while line-by-line reading, such as:
$cat fileaaaabbbb fff gggcccc dddd$ cat file | while read line; do echo $line; doneaaaabbbb fff gggcccc dddd$ for line in $(
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.