Shell和其他普遍的语言一样,在循环语句这方面,也是有for,while和until。执行的逻辑上大概都是类似的。如果其他语言有一定基础,就很容易理解了,再学一下语法,就可以灵活使用了。
For为标志的循环语句:
这个和python的for很类似。都是涉及一个列表
具体的语法格式如下:
for 变量 in 列表
do
command1
command2
...
commandN
done
例子:
一、顺序输出数字
[root@localhost shell_protest]# sh for.sh
value is 1
value is 2
value is 3
value is 4
value is 5
-----------------Source-------------------------
[root@localhost shell_protest]# cat for.sh
for i in 1 2 3 4 5
do
echo value is $i
done
二、打印某文件夹下所有文件名即文件类型
[root@localhost shell_protest]# sh list.sh
please input the path/
bin ---- symbolic link to `usr/bin'
boot ---- directory
dev ---- directory
etc ---- directory
home ---- directory
lib ---- symbolic link to `usr/lib'
lib64 ---- symbolic link to `usr/lib64'
media ---- directory
mnt ---- directory
opt ---- directory
proc ---- directory
root ---- directory
run ---- directory
sbin ---- symbolic link to `usr/sbin'
shell_protest ---- directory
srv ---- directory
sys ---- directory
tmp ---- sticky directory
usr ---- directory
var ---- directory
----------------Source--------------------
[root@localhost shell_protest]# cat list.sh
#!/bin/bash
read -p "please input the path" path
cd $path||exit
for i in `ls $path`
do
echo "$i ---- `file $i|awk -F: '{print $2}'`"
done
三、显示主目录下以 .bash 开头的文件:
[root@localhost shell_protest]# sh file.sh
/root/.bash_history
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
------------------Source-----------------------------
[root@localhost shell_protest]# cat file.sh
#!/bin/bash
for file in $HOME/.bash*
do
echo $file
done
然后,我们再来说一下while。
while是当条件为真的时候循环,一直到条件为false
在while我会分享一个比较实用好玩的例子:
不过,先来说一下语法吧!
while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为:
while command
do
Statement(s) to be executed if command is true
done
分享一个示例:
用处是执行他,然后输入我们想输入的东西,重定向到所需文件中
[root@localhost shell_protest]# sh inputfile.sh
which file(fullpath) you want to input
/tmp/hello
input CTRL + D to stop
enter your want to save in $filename
hhhhhhhaoloo
beye
!!
------------------Result-----------------------
[root@localhost shell_protest]# cat /tmp/hello
hhhhhhhaoloo
beye
!!
------------------Source-------------------------
[root@localhost shell_protest]# cat inputfile.sh
#!/bin/bash
echo 'which file(fullpath) you want to input'
read filename||exit
echo 'input CTRL + D to stop'
echo 'enter your want to save in $filename'
while read content
do
echo $content >>$filename
done
到最后的until了。
until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。一般while循环优于until循环,但在某些时候,也只是极少数情况下,until 循环更加有用。
语法格式:
until command
do
Statement(s) to be executed until command is true
done
示例:输出0-9
[root@localhost shell_protest]# sh until.sh
0
1
2
3
4
5
6
7
8
9
-----------------Source-----------------------
[root@localhost shell_protest]# cat until.sh
#!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
既然有循环,那么也会有跳出循环的语句
break和continue应该是我们首先浮现在脑海中的两个词
break和continue在Shell里面大多数语言都一样,都是同样的作用,continue是跳过一次循环,而break是跳出循环!
但是这里break有新的用法。就是可以指定跳出多少层循环!
语法:
break n
continue也是一样的,也有跳过第几层循环的用法:
continue n
先来一起看看break的一般用法吧:
下面是一个猜数字的程序:
[root@localhost shell_protest]# sh guess.sh
guess a number between 0-9 : 1
wrong!
guess a number between 0-9 : 1
wrong!
guess a number between 0-9 : 1
welldone
-------------------------------------
[root@localhost shell_protest]# cat guess.sh
#!/bin/bash
while :
do
read -p "guess a number between 0-9 : " num
if [ $num -eq `echo $RANDOM|grep ^. -o` ]
then
echo welldone
break
else
echo wrong!
fi
done
break n的示例:
[root@localhost shell_protest]# sh breakn.sh
outside
inside
-----------------Source---------------------------
[root@localhost shell_protest]# cat breakn.sh
#!/bin/bash
while true
do
echo "outside"
while true
do
echo "inside"
break 2
done
done
#假如不是break 2,是break。循环运行起来是个死循环