bash shell while语法
2016-09-27 11:18
141 查看
在编写脚本时,一定要注意空格
基本语法:
while [ condition ]
do
command1
command2
command3
done
counter=$1
factorial=1
while [ $counter -gt 0 ]
do
factorial=$(( $factorial * $counter ))
counter=$(( $counter - 1 ))
done
echo $factorialTo run just type:
Output:120While loops are frequently used for reading data line by line from file:#!/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<&3You 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
.......
..
while :
do
echo "infinite loops [ hit CTRL+C to stop]"
done
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.
doneIn this example, the break statement will skip the while loop when user enters -1, otherwise it will keep adding two numbers:#!/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
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
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
基本语法:
while [ condition ]
do
command1
command2
command3
done
condition为true时命令1到命令3将会一直执行,知道条件为false ,例如:
#!/bin/bash x=1 while [ $x -le 5 ] do echo "Welcome $x times" x=$(( $x + 1 )) doneHere 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 $factorialTo run just type:
$ chmod +x script.sh $ ./script.sh 5
Output:120While loops are frequently used for reading data line by line from file:#!/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<&3You 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
.......
..
How do I use while as infinite loops?
Infinite for while can be created with empty expressions, such as:#!/bin/bashwhile :
do
echo "infinite loops [ hit CTRL+C to stop]"
done
Conditional while loop exit with break statement
You can do early exit with the break statement inside the whil loop. You can exit from within a WHILE using break. General break statement inside the while loop is as follows: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.
doneIn this example, the break statement will skip the while loop when user enters -1, otherwise it will keep adding two numbers:#!/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
Early continuation with the continue statement
To resume the next iteration of the enclosing WHILE loop use the continue statement as follows: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
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
相关文章推荐
- bash shell while语法
- shell编程——if语句 if -z -n -f -eq -ne -lt
- shell编程——if语句 if -z -n -f -eq -ne -lt
- 如何在shell中处理异常(Part 2)
- 99乘法表(bash)
- 在Windows server 2016 RTM下用PoweShell无人值守安装配置Exchange 2016 CU3
- 可移动式编程平台 c,c++,python等可用。轻量迅速便捷。可移动shell
- shell中的${},##和%%的使用
- Bash简明教程--变量
- JMeter学习-037-JMeter调试工具之四-BeanShell+jmeter.log
- 如何短时间内学好一门语言 shell脚本语言为例
- nohup如何记录进程号
- shell脚本基础——常用的sed命令举例
- linux shell中单引号、双引号、反引号、反斜杠的区别
- 通配符和正则表达式的区别
- linux shell的here document用法
- Linux Bash Shell 快速入门
- ubuntu 14.04修改用户组成员默认的Shell
- Linux程序编写shell script的格式
- powershell递归删除文件