BASH/While
来自康健生活
< BASH
While
bash while循环是一个控制流语句,它允许代码或命令根据给定的条件重复执行。语法如下:
while [ condition ]
do
command1
command2
command3
done
- while循环的参数可以是任何布尔表达式。当条件从不计算为false时会发生无限循环。
循环将在屏幕上打印欢迎5次
#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done
|
$ ./while.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
|
循环计算阶乘
#!/bin/bash
counter=$1
factorial=1
while [ $counter -gt 0 ]
do
factorial=$(( $factorial * $counter ))
counter=$(( $counter - 1 ))
done
echo $factorial
|
$ ./while.sh 5
120
|
循环用于从文件逐行读取数据
#!/bin/bash
FILE=$1
# read $FILE using the file descriptors
exec 3<&0
exec 0<$FILE
while read line
do
# use $line variable to process line
echo $line
done
exec 0<&3
break
使用whil循环内的break语句提前退出。
while [ condition ]
do
statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any.
statements2
if (disaster-condition)
then
break #Abandon the while lopp.
fi
statements3 #While good and, no disaster-condition.
done
当用户输入-1时,break语句将跳过while循环,否则它将继续添加两个数字
#!/bin/bash
while :
do
read -p "Enter two numnbers ( - 1 to quit ) : " a b
if [ $a -eq -1 ]
then
break
fi
ans=$(( a + b ))
echo $ans
done
|
$ ./while.sh
Enter two numnbers ( - 1 to quit ) : -1
|
continue
要恢复封装WHILE循环的下一次迭代,请使用continue语句。
while [ condition ]
do
statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any.
statements2
if (condition)
then
continue #Go to next iteration of I in the loop and skip statements3
fi
statements3
done
无限循环(死循环)
空表达式
#!/bin/bash
while :
do
echo "infinite loops [ hit CTRL+C to stop]"
done
true
while true
do
语句
done
[0]或者[1]
while [0]
do
语句
done
|
while [1]
do
语句
done
|
use the following syntax
| 输入 | 输入 |
|---|---|
while :; do echo 'Hit CTRL+C'; sleep 1; done
|
while true; do echo 'Hit CTRL+C'; sleep 1; done
|
To cycle through a loop forever using for loop
for (( ; ; ))
do
echo "Pres CTRL+C to stop..."
sleep 1
done
|
How Do I Escape the Loop
A for or while loop may be escaped with a break statement when certain condition is satisfied:
| 输入 | 输入 |
|---|---|
### for loop example ###
for (( ; ; ))
do
echo "Pres CTRL+C to stop..."
sleep 1
if (disaster-condition)
then
break #Abandon the loop.
fi
done
|
### while loop example ###
while :
do
echo "Pres CTRL+C to stop..."
sleep 1
if (disaster-condition)
then
break #Abandon the loop.
fi
done
|
You can also use the case statement to esacpe with a break statement:
while :
do
### add some input and output here ###
case $var in
yes) do something ;;
no) do something ;;
quit) break ;; ##Abandon the loop.
ease
done
|
Example
A sample shell script to demonstrate the actual usage of an infinite loop and the break statement
#!/bin/bash
# Purpose: Display various options to operator using menus
# Author: Vivek Gite < vivek @ nixcraft . com > under GPL v2.0+
# ---------------------------------------------------------------------------
# capture CTRL+C, CTRL+Z and quit singles using the trap
trap '' SIGINT
trap '' SIGQUIT
trap '' SIGTSTP
# display message and pause
pause(){
local m="$@"
echo "$m"
read -p "Press [Enter] key to continue..." key
}
# set an
while :
do
# show menu
clear
echo "---------------------------------"
echo " M A I N - M E N U"
echo "---------------------------------"
echo "1. Show current date/time"
echo "2. Show what users are doing"
echo "3. Show top memory & cpu eating process"
echo "4. Show network stats"
echo "5. Exit"
echo "---------------------------------"
read -r -p "Enter your choice [1-5] : " c
# take action
case $c in
1) pause "$(date)";;
2) w| less;;
3) echo '*** Top 10 Memory eating process:'; ps -auxf | sort -nr -k 4 | head -10;
echo; echo '*** Top 10 CPU eating process:';ps -auxf | sort -nr -k 3 | head -10;
echo; pause;;
4) netstat -s | less;;
5) break;;
*) Pause "Select between 1 to 5 only"
esac
done
No categories