How Does Shell Script Looping Work?(shell程序设计中的循环)
2011-09-04 12:04
477 查看
Shell scripts written in Bash can implementlooping, or iteration, with thewhile,until, andforconstructs. In each case, a block of code is executed repeatedly
until a loop exit condition is satisfied. The script then continues on from that point.
The while Statement
In awhileloop, the block of code between thedoanddonestatements is executed so long as the conditional expression is true. Think of it as saying, "Executewhilethis condition remains true." Here's
an example:
while [ "$*" != "" ]
do
echo "Argument value is: $1"
shift
done
This trivial example prints the value of each argument passed to the shell script. Translated to English, thewhilecondition says to continue so long as the input argument string is not null. You could also code thewhilestatement
as
while [ -n"$*"]
but I think the first method is much easier to read and understand.
You might think that this loop would continue to print the first argument ($1) forever and ever, since you don't expect the value of the$*variable (the list of arguments from the command line) to change during the course
of running the script. You'd be right, except that I slipped theshiftcommand into the body of the loop.
Whatshiftdoes is discard the first argument and reassign all the$nvariables--so the new$1variable gets the value that used to be in$2,and so on. Accordingly, the value
in$*gets shorter and shorter each time through the loop, and when it finally becomes null, the loop is done.
The until Statement
Theuntilconstruct works almost exactly the same aswhile. The only difference is thatuntilexecutes the body of the loop so long as the conditional expression is false, whereaswhileexecutes
the body of the loop so long as the conditional expression is true. Think of it as saying, "Executeuntilthis condition becomes true."
Let's code the previous example using anuntilloop this time and making it a little fancier by adding acountervariable:
count=1
until [ "$*" = "" ]
do
echo "Argument number $count : $1 "
shift
count=`expr $count + 1`
done
Again, you could have coded theuntilstatement as
until [ -z"$*"]
but I recommend not using the-nand-zoperators because it's harder to remember what they do.
The only new concept here is the strange-looking line that increments the counter:
count=`expr $count + 1`
Theexprcommand signals to the shell that we're about to perform a mathematical calculation instead of a string operation. And the doodads that look kind of like single quotation marks are not--they're thebacktick(`)
character, found to the left of the number 1 key on most keyboards. By enclosing an expression in backticks, you tell the shell to assign the result of a Linux command to a variable, instead of printing it to the screen.
Note: The spaces on either side of the plus sign are required.
The for Statement
Theforstatement is yet another way to implement a loop in a shell script. The general form of theforconstruct is shown here:
for item in list
do
something useful with $item
done
Each time through the loop, the value of theitemvariable is assigned to thenth item in the list. When you've processed all the items in the list, the loop is done. Here's an example similar to theuntilandwhileloops
you saw earlier in this section:
for item in"$@"
do
echo "Argument value is: $item"
done
Note that in this example, I used the special variable$@instead of$*as the list of arguments. The variables$@and$*both contain the command line arguments, but theforcommand
expects an array (a list of items) as input.
Read more:http://lowfatlinux.com/linux-script-looping.html#ixzz1Wx9zHh6w
until a loop exit condition is satisfied. The script then continues on from that point.
The while Statement
In awhileloop, the block of code between thedoanddonestatements is executed so long as the conditional expression is true. Think of it as saying, "Executewhilethis condition remains true." Here's
an example:
while [ "$*" != "" ]
do
echo "Argument value is: $1"
shift
done
This trivial example prints the value of each argument passed to the shell script. Translated to English, thewhilecondition says to continue so long as the input argument string is not null. You could also code thewhilestatement
as
while [ -n"$*"]
but I think the first method is much easier to read and understand.
You might think that this loop would continue to print the first argument ($1) forever and ever, since you don't expect the value of the$*variable (the list of arguments from the command line) to change during the course
of running the script. You'd be right, except that I slipped theshiftcommand into the body of the loop.
Whatshiftdoes is discard the first argument and reassign all the$nvariables--so the new$1variable gets the value that used to be in$2,and so on. Accordingly, the value
in$*gets shorter and shorter each time through the loop, and when it finally becomes null, the loop is done.
The until Statement
Theuntilconstruct works almost exactly the same aswhile. The only difference is thatuntilexecutes the body of the loop so long as the conditional expression is false, whereaswhileexecutes
the body of the loop so long as the conditional expression is true. Think of it as saying, "Executeuntilthis condition becomes true."
Let's code the previous example using anuntilloop this time and making it a little fancier by adding acountervariable:
count=1
until [ "$*" = "" ]
do
echo "Argument number $count : $1 "
shift
count=`expr $count + 1`
done
Again, you could have coded theuntilstatement as
until [ -z"$*"]
but I recommend not using the-nand-zoperators because it's harder to remember what they do.
The only new concept here is the strange-looking line that increments the counter:
count=`expr $count + 1`
Theexprcommand signals to the shell that we're about to perform a mathematical calculation instead of a string operation. And the doodads that look kind of like single quotation marks are not--they're thebacktick(`)
character, found to the left of the number 1 key on most keyboards. By enclosing an expression in backticks, you tell the shell to assign the result of a Linux command to a variable, instead of printing it to the screen.
Note: The spaces on either side of the plus sign are required.
The for Statement
Theforstatement is yet another way to implement a loop in a shell script. The general form of theforconstruct is shown here:
for item in list
do
something useful with $item
done
Each time through the loop, the value of theitemvariable is assigned to thenth item in the list. When you've processed all the items in the list, the loop is done. Here's an example similar to theuntilandwhileloops
you saw earlier in this section:
for item in"$@"
do
echo "Argument value is: $item"
done
Note that in this example, I used the special variable$@instead of$*as the list of arguments. The variables$@and$*both contain the command line arguments, but theforcommand
expects an array (a list of items) as input.
Read more:http://lowfatlinux.com/linux-script-looping.html#ixzz1Wx9zHh6w
相关文章推荐
- 安装ipython,使用scrapy shell来验证xpath选择的结果 | How to install iPython and how does it work with Scrapy Shell
- 程序设计基本结构----顺序、选择与循环
- How does deep residual learning work?
- 自建Yii项目进入shell 出现Error: index.php does not exist or is not an entry script file.
- 第二章 shell程序设计(3)
- How does the Three-state workflow work?三态工作流
- How does anti-virus software work?(转载)
- How to build docker registry for local use by shell script
- [quote] How to comment multi-lines in bash shell script.
- how does wifi work?
- angular this vs $scope (How does 'this' and $scope work in AngularJS controllers?)
- 使用汇编语言编写程序,设计一个用8051单片机控制的循环彩灯控制系统
- How the heck does async/await work in Python 3.5
- JavaScript: How does 'new' work internally
- How ARC does its work?
- 实验三 shell程序设计
- Shell实现程序造死循环的几种方法示例
- 几个shell程序设计小知识
- 转载:How does Google Map My Location work?
- How does a computer virus scan work?(转载)