Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the while loop in Linux shell?

Shulou Source: shulou.com Published: 2022-06-01 21:48:02 10月04日 Update

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.

Tags: Loop week format code statement example content common use parameter command place instance file more condition knowledge article structure industry grammar Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info MariaDB OPPO Reno Shulou Technology