In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about what the while cycle in Linux shell is like. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
While loop is also a common loop structure in shell, which has many similarities with the syntax of other languages, but there are also some differences.
Common format
Format-while condition
Do
Statement
Done
Format two-dead loop while true
Do
Statement
Done
Format triple dead loop while:
Do
Statement
Done
Format four endless loop while [1]
Do
Statement
Done
Format five endless loop while [0]
Do
Statement
Done
Use the sample example-Bash code
COUNTER=0
While [$COUNTER-lt 10]; do
Echo The counter is $COUNTER
Let COUNTER=COUNTER+1
Done
[root@jfht ~] # COUNTER=0 [root@jfht ~] # while [COUNTER > let COUNTER=COUNTER+1 > doneThe counter is 0The counter is 1The counter is 2The counter is 3The counter is 4The counter is 5The counter is 6The counter is 7The counter is 8The counter is 9 [root@jfht ~] #
It is better to use the for loop instead of the while loop.
Bash code
For ((COUNTER=0; COUNTER
Do
Echo The counter is $COUNTER
Done
[root@jfht] # for ((COUNTER=0; COUNTER > do > echo The counter is $COUNTER > doneThe counter is 0The counter is 1The counter is 2The counter is 3The counter is 4The counter is 5The counter is 6The counter is 7The counter is 8The counter is 9 [root@jfht ~] #
Example two Bash code
While true
Do
Date
Sleep 1
Done
[root@jfht] # while true > do > date > sleep 1 > done2010 October 10 Sunday 16:35:22 CST2010 October 10 Sunday 16:35:23 CST2010 October 10 Sunday 16:35:24 CST2010 October 10 Sunday 16:35:25 CST2010 October 10 Sunday 16:35:26 CST October 10 Sunday 10 October 2010 16:35:27 CSTCtrl+C [root@jfht] #
Example 3 read the input Java code
While read line
Do
Echo $line
Done
[root@jfht ~] # while read line > do > echo $line > donehellohelloworldworldCtrl+D [root@jfht ~] #
Example 4 deals with the command line parameter file while_4.sh
Bash code
#! / bin/sh usage () {echo "usage: $0 [- a] [- e] [- f] [- h] [- d] [- s] [- Q] [- x]} while getopts ae:f:hd:s:qx: option do case" ${option} "in a) ALARM=" TRUE ";; e) ADMIN=$ {OPTARG};; d) DOMAIN=$ {OPTARG};; f) SERVERFILE=$OPTARG S) WHOIS_SERVER=$OPTARG;; Q) QUIET= "TRUE";; x) WARNDAYS=$OPTARG;;\?) Usage; exit 1 usage; esac done echo "ALARM=$ALARM" echo "ADMIN=$ADMIN" [root@jfht ~] # cat whilebacks 4.shusage () {echo "usage: $0 [- a] [- e] [- f] [- h] [- d] [- s] [- Q] [- x]"} while getopts ae:f:hd:s:qx: optiondo case "{OPTARG};; d) DOMAIN=OPTARG;; s) WHOIS_SERVER=OPTARG \?) Usage; exit 1; esacdoneecho "ALARM=ADMIN" [root@jfht] # chmod + x while_ 4.sh [root @ jfht ~] #. / while_4.shALARM=ADMIN= [root @ jfht ~] #. / while_4.sh-aALARM=TRUEADMIN= [root@jfht ~] #. / while_4.sh-e hyALARM=ADMIN=hy after reading the above, do you have any further understanding of the while cycle in Linux shell? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.