linux while 迴圈
13. 07.
Bash While Loop Example – Bash While 迴圈例項
Q. How do I use bash while loop to repeat certain task under Linux / UNIX operating system?
A. Bash while loop is a control flow statement that allows code or
commands to be executed repeatedly based on a given condition. For
example, run echo command 5 times or read text file line by line or
evaluate the options passed on the command line for a script.
while loop syntax
while [ condition ]
do
command1
command2
command3
done
command1 to command3 will be executed repeatedly till condition is true. The argument for a while loop can be any boolean expression. Infinite loops occur when the conditional never evaluates to false. For example following while loop will print welcome 5 times on screen:
#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done
Here is a sample shell code to calculate factorial using while loop:
#!/bin/bash
counter=$1
factorial=1
while [ $counter -gt 0 ]
do
factorial=$(( $factorial * $counter ))
counter=$(( $counter - 1 ))
done
echo $factorial
To run just type:
$ chmod +x script.sh
$ ./script.sh 5
Output:
120
While loops are frequently used for reading data line by line from file:
#!/bin/bash
FILE=$1
# read $FILE using the file descriptors
exec 3exec 0while read line
do
# use $line variable to process line
echo $line
done
exec 0
You can easily evaluate the options passed on the command line for a script. using while loop:
......
..
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;;
esac
done
.......
..
作者: | 可以轉載, 轉載時務必以超連結形式標明文章和作者資訊及版權宣告
網址:
Tags: , , ,
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-668909/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- while迴圈以及do while迴圈While
- C語言——迴圈結構(for迴圈,while迴圈,do-while迴圈)C語言While
- while迴圈While
- PHP For & While 迴圈PHPWhile
- Java 迴圈 - for, while 及 do…whileJavaWhile
- 04流程控制 for迴圈,while迴圈While
- python while迴圈PythonWhile
- while迴圈補充While
- Linux while 迴圈中使用ssh問題LinuxWhile
- Java迴圈結構-for,while及do…whileJavaWhile
- Java while和do while迴圈詳解JavaWhile
- JavaScript中的while迴圈JavaScriptWhile
- python-while迴圈PythonWhile
- C語言程式設計學習中while迴圈和do……while迴圈C語言程式設計While
- Java 迴圈結構 - for, while 及 do...whileJavaWhile
- c#入門-while迴圈C#While
- java學習之while迴圈JavaWhile
- mysql 中 while 迴圈的用法。MySqlWhile
- C#程式設計基礎第七課:C#中的基本迴圈語句:while迴圈、do-while迴圈、for迴圈、foreach迴圈的使用C#程式設計While
- Object-C,迴圈語句for,while,do-whileObjectWhile
- python04: while迴圈語句 break continue for in 迴圈PythonWhile
- python 基礎習題6--for迴圈和while迴圈PythonWhile
- while迴圈和do迴圈、緩衝區、一維陣列While陣列
- Java入門學習-學習if & else,for迴圈,foreach迴圈,while迴圈的用法。JavaWhile
- C#練習,應用for,while,do-while迴圈C#While
- web前端開發教程-while迴圈Web前端While
- 15-python之while迴圈PythonWhile
- java死迴圈while(true)vsfor(;;)JavaWhile
- while + else 使用,while死迴圈與while的巢狀,for迴圈基本使用,range關鍵字,for的迴圈補充(break、continue、else) ,for迴圈的巢狀,基本資料型別及內建方法While巢狀資料型別
- Java基礎 迴圈語句 for while do.....while語句JavaWhile
- 探討兩種迴圈表示方法的區別,while迴圈與for迴圈的小總結While
- Python基礎-While迴圈語句PythonWhile
- Python學習-while迴圈練習PythonWhile
- python基礎 while迴圈練習PythonWhile
- 資料型別——集合與while迴圈資料型別While
- javascript基礎(控制流程(迴圈 while,for))(十二)JavaScriptWhile
- PHP快速入門教程:WHILE迴圈示例PHPWhile
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-WHILE迴圈SQLWhile