在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ Shell 循環(huán)類型
Shell 輸入/輸出重定向
Shell 循環(huán)類型
Shell是什么?
Shell 特殊變量
Shell 算術運算符示例
Shell 關系運算符示例
Shell 替代
Shell 函數(shù)
Shell 條件語句
Shell 聯(lián)機幫助
Shell 數(shù)組/Arrays
Shell 布爾運算符范例
Shell
Shell if...elif...fi 語句
Shell case...esac 語句
Shell 使用Shell變量
Shell 文件測試符例子
Shell 基本運算符
Korn Shell 運算符
Shell 字符串運算范例
Shell while 循環(huán)
Shell 引用機制
Shell if...else...fi 語句
Shell select 循環(huán)
C Shell運算符
Shell 循環(huán)控制break/continue
Shell for循環(huán)
Shell until 循環(huán)
Shell if...fi語句

Shell 循環(huán)類型

循環(huán)是一個強大的編程工具,使您能夠重復執(zhí)行一組命令。在本教程中,您將學習以下類型的循環(huán)Shell程序:

你會根據(jù)不同情況使用不同的循環(huán)。例如用 while 循環(huán)執(zhí)行命令,直到給定的條件下是 ture ,循環(huán)直到執(zhí)行到一個給定的條件為 false。

有良好的編程習慣,將開始使用情況的基礎上適當?shù)难h(huán)。這里while和for循環(huán)類似在大多數(shù)其他的編程語言,如C,C++ 和 Perl 等。

嵌套循環(huán):

所有支持嵌套循環(huán)的概念,這意味著可以把一個循環(huán)內(nèi)其他類似或不同的循環(huán)。這種嵌套可以去高達無限數(shù)量的時間根據(jù)需要。

嵌套的while循環(huán)和類似的方式,可以嵌套其他循環(huán)的基礎上的編程要求下面是一個例子:

嵌套while循環(huán):

作為另一個while循環(huán)的身體的一部分,這是可以使用一個while循環(huán)。

語法:

while command1 ; # this is loop1, the outer loop
do
   Statement(s) to be executed if command1 is true

   while command2 ; # this is loop2, the inner loop
   do
      Statement(s) to be executed if command2 is true
   done

   Statement(s) to be executed if command1 is true
done

例如:

這里是循環(huán)嵌套一個簡單的例子,讓我們添加另一個倒計時循環(huán)內(nèi)的循環(huán),數(shù)到九:

#!/bin/sh

a=0
while [ "$a" -lt 10 ]    # this is loop1
do
   b="$a"
   while [ "$b" -ge 0 ]  # this is loop2
   do
      echo -n "$b "
      b=`expr $b - 1`
   done
   echo
   a=`expr $a + 1`
done

這將產(chǎn)生以下結果。重要的是要注意 echo -n 是如何工作。在這里,-n選項echo ,以避免打印一個新行字符。

0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0