[Posts] the shell loop for / do / done and while / do / done and break, continue

shell loop for / do / done and while / do / done as well as break, continue

https://blog.csdn.net/weixin_38280090/article/details/81843264

 


Original tongue ear Posted 2018-08-19 22:58:17 reading number 15072 collection
launched

for / do / DONE
Shell script for loop structures and C languages are very different, which is similar to the foreach loop in some programming languages. E.g:

! # / Bin / SH

for in the Apple Banana FRUIT PEAR; do
echo "$ FRUIT like the I"
DONE
FRUIT is a loop variable, the first cycle the value is $ FRUIT apple, the second value is the banana, third times the value of a pear. As another example, to chap0 in the current directory, chap1, chap2 such as file name to chap0 ~, chap1 ~, chap2 ~ etc. (As usual, at the end there - character file name means a temporary files), this command can be written like this:


for FILENAME in chap ?; do mv $ FILENAME $ FILENAME ~; done
can also be written like this:

`FILENAME in LS CHAP for`;? do mv $ FILENAME $ FILENAME ~; DONE
the while / do / DONE
the while usage and C language similar. For example, a password validation script:

! # / Bin / SH

echo "the Enter password:"
Read TRY
the while; do [ "TRY $" = "Secret"!]
Echo "Sorry, the try Again"
Read TRY
DONE
number of arithmetic operations by the following example of the control cycle:

! # / Bin / SH

the COUNTER. 1 =
the while [ "$ the COUNTER" -LT-10]; do
echo "Go Here Again WE"
the COUNTER = $ (($ + the COUNTER. 1))
DONE
Shell there until loop, a C-like do ... while loop. This chapter is omitted.

break and Continue
break [n-] out of layers can be specified cycle, continue cycle skip this step, not out of the entire cycle.

break out, continue skipped.
----------------
Disclaimer: This article is CSDN blogger original article "tongue ear", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/weixin_38280090/article/details/81843264

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12084152.html