In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you how to understand the linux for cycle. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The for loop is the most commonly used structure in Linux shell. For loops have three structures: one is a list for loop; the second is a for loop without a list; and the third is a C-like for loop.
For var in list
Do
Commands
Done
#! / bin/bash
For test in alabama alaska arizona arkansas california colorado
Do
Echo the next state is $test
Done
>
The next state is alabama
The next state is alaska
The next state is arizona
The next state is arkansas
The next state is california
The next state is colorado
#! / bin/bash
For test in alabama alaska arizona arkansas california colorado
Do
Echo "the next state is $test"
Done
Echo "The las state we visited was $test"
Test=connecticut
Echo "wait, now we're visiting $test"
>
The next state is alabama
The next state is alaska
The next state is arizona
The next state is arkansas
The next state is california
The next state is colorado
The las state we visited was colorado
Wait, now we're visiting connecticut
#! / bin/bash
For test in I don't know if this'll work
Do
Echo "word:$test"
Done
>
Word:I
Word:dont know if thisll
Word:work
#! / bin/bash
For test in nevada "New Hampshire"New Mexico"New York"
Do
Echo "Now going to $test"
Done
>
Now going to nevada
Now going to New Hampshire
Now going to New Mexico
Now going to New York
#! / bin/bash
List= "Alabama ALaska Arizona Arkansas Colorado"
List=$list "Connecticut"
For state in $list
Do
Echo "Have you ever visited $state?"
Done
>
Have you ever visited Alabama?
Have you ever visited ALaska?
Have you ever visited Arizona?
Have you ever visited Arkansas?
Have you ever visited Colorado?
Have you ever visited Connecticut?
#! / bin/bash
File= "states"
For state in `cat $file`
Do
Echo "Visit beautiful $state"
Done
# cat states
>
Alabama
Alaska
Dddfac
Dfsase
Cvcvss
Dfqzcvc
Ddddwq
Bnnn
Qqojfaso
>
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful dddfac
Visit beautiful dfsase
Visit beautiful cvcvss
Visit beautiful dfqzcvc
Visit beautiful ddddwq
Visit beautiful bnnn
Visit beautiful qqojfaso
#! / bin/bash
IFS=$'\ n'
File= "states"
For state in `cat $file`
Do
Echo "visit beautiful $state"
Done
>
# cat states
>
A labama
Alaska
Dddfac
Dfsase
Cvcvss
Dfqzcvc
Ddddwq
Bnnn
Qqojfaso
>
Visit beautiful A labama
Visit beautiful Alaska
Visit beautiful dddfac
Visit beautiful dfsase
Visit beautiful cvcvss
Visit beautiful dfqzcvc
Visit beautiful ddddwq
Visit beautiful bnnn
Visit beautiful qqojfaso
When dealing with long scripts, you may need to change the value of IFS in one place, then forget about it and think it is the default value elsewhere in the script. A simple practice to refer to:
IFS.OLD=$IFS
IFS=$'\ n'
IFS=$IFS.OLD
#! / bin/bash
For file in / root/*
Do
If [- d "$file"] then
Echo "$file is a directory"
Elif [- f "$file"] then
Echo "$file is a file"
Fi
Done
#! / bin/bash
For file in / root/.b* / home/rich/badtest
Do
If [- d "$file"] then
Echo "$file is a directory"
Elif [- f "$file"] then
Echo "$file is a file"
Else
Echo "$file doesn't exist"
Fi
Done
C-like language style
#! / bin/bash
For ((iTun1; ido)
Echo "The next nuber is $I"
Done
>
The next nuber is 1
The next nuber is 2
The next nuber is 3
The next nuber is 4
The next nuber is 5
The next nuber is 6
The next nuber is 7
The next nuber is 8
The next nuber is 9
The next nuber is 10
#! / bin/bash
For ((axi1, baux10; a do)
Echo "$a-$b"
Done
>
1-10
2-9
3-8
4-7
5-6
6-5
7-4
8-3
9-2
10-1
11-0
12-1 above is how the linux for cycle shared by the editor is understood. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.