一、for命令
for命令用于创建一系列值重复的循环。for命令的基本格式为:
for var in list
do
commands
done
(1)读取列表中的值
[root@ceph01 test]# cat for-read-list.sh
#!/bin/bash
# basic for comamnd
for test in Alabama Alaska Arizona Arkansas California colorado
do
echo The next state is $test
done
运行脚本:
[root@ceph01 test]# ./for-read-list.sh
The next state is Alabama
The next state is Alaska
The next state is Arizona
The next state is Arkansas
The next state is California
The next state is colorado
(2)读取列表中的复杂值
[root@ceph01 test]# cat for-read-list2.sh
#!/bin/bash
# another example of how not to use the for command
for test in I don't know if this'll work
do
echo "word:$test"
done
运行脚本:
[root@ceph01 test]# ./for-read-list2.sh
word:I
word:dont know if thisll
word:work
shell看到列表当中的单引号,并试图用它们来定义一个单独的数据值,它的确破坏了这个过程。以下两种可以解决
使用转义字符(反斜杠符号)来转义单引号
使用双引号来定义使用单引号的值
[root@ceph01 test]# cat for-read-list2.sh
#!/bin/bash
# another example of how not to use the for command
for test in I don\'t know if "this'll" work
do
echo "word:$test"
done
[root@ceph01 test]# ./for-read-list2.sh
word:I
word:don't
word:know
word:if
word:this'll
word:work
(3)从变量读取列表
[root@ceph01 test]# cat for-read-list3.sh
#!/bin/bash
# using a variable to hold the list
list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut"
for state in $list
do
echo "Have you ever visited $state ?"
done
运行脚本:
[root@ceph01 test]# ./for-read-list3.sh
Have you ever visited Alabama ?
Have you ever visited Alaska ?
Have you ever visited Arizona ?
Have you ever visited Arkansas ?
Have you ever visited Colorado ?
Have you ever visited Connecticut ?
(4)读取命令中的值
[root@ceph01 test]# cat for-read-list4.sh
#!/bin/bash
# reading values from a file
file="states"
for state in 'cat $file'
do
echo "Visit beautiful $state"
done
[root@ceph01 test]# cat states
Alabama
Alaska
Girl
Boy
运行脚本:
[root@ceph01 test]# ./for-read-list4.sh
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful Girl
Visit beautiful Boy
(5)改变字段分隔符
bash shell将下面的字符看作字段分隔符:
空格
制表符
换行符
[root@ceph01 test]# cat for-read-list5.sh
#!/bin/bash
# reading values from a file
file="states"
IFS=$'\n'
for state in `cat $file`
do
echo "Visit beautiful $state"
done
运行脚本:
[root@ceph01 test]# ./for-read-list5.sh
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful Girl
Visit beautiful Boy
(6)使用通配符读取目录
可以使用for命令自动迭代文件的目录,必须在文件或路径中使用通配符。
-d查看文件是否是目录,-f查看文件是否是文件
[root@ceph01 test]# cat for-read-list6.sh
#!/bin/bash
# iterate through all the files in a directory
for file in /home/test/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done
运行脚本:
[root@ceph01 test]# ./for-read-list6.sh
/home/test/bc1.sh is a file
/home/test/bc.sh is a file
/home/test/bool.sh is a file
/home/test/for-read-list4.sh is a file
/home/test/for-read-list5.sh is a file
/home/test/for-read-list6.sh is a file
/home/test/test-value.sh is a file
二、C式的for命令
(1)shell中的for命令
bash中c式for循环的基本格式:
for (( variable assignment; condition; iteration process ))
注:变量的赋值可以包含空格;
条件中的变量不以美元符号做前缀
迭代处理不使用expr命令格式
[root@ceph01 test]# cat for-C-style.sh
#!/bin/bash
# testing the C-style for loop
for (( i=1; i <= 10; i++ ))
do
echo "The next number is $i"
done
运行脚本:
[root@ceph01 test]# ./for-C-style.sh
The next number is 1
The next number is 2
The next number is 3
The next number is 4
The next number is 5
The next number is 6
The next number is 7
The next number is 8
The next number is 9
The next number is 10
(2)使用多个变量
[root@ceph01 test]# cat for-C-style1.sh
#!/bin/bash
# multiple variables
for (( a=1, b=10; a <= 10; a++,b-- ))
do
echo "$a - $b"
done
运行脚本:
[root@ceph01 test]# ./for-C-style1.sh
1 - 10
2 - 9
3 - 8
4 - 7
5 - 6
6 - 5
7 - 4
8 - 3
9 - 2
10 - 1
三、while命令
(1)while的基本格式
while命令的格式是:
while test command
do
other commands
done
[root@ceph01 test]# cat while.sh
#!/bin/bash
# while command test
var1=10
while [ $var1 -gt 0 ]
do
echo $var1
var1=$[ $var1 - 1 ]
done
运行脚本:
[root@ceph01 test]# ./while.sh
10
9
8
7
6
5
4
3
2
1
(2)使用多条测试命令
[root@ceph01 test]# cat while1.sh
#!/bin/bash
# testing a multicommand while loop
var1=10
while echo $var1
[ $var1 -ge 0 ]
do
echo "This is inside the loop"
var1=$[ $var1 - 1 ]
done
运行脚本:
[root@ceph01 test]# ./while1.sh
10
This is inside the loop
9
This is inside the loop
8
This is inside the loop
7
This is inside the loop
6
This is inside the loop
5
This is inside the loop
4
This is inside the loop
3
This is inside the loop
2
This is inside the loop
1
This is inside the loop
0
This is inside the loop
-1
四、until命令
until命令的格式是:
until test commands
do
other commands
done
[root@ceph01 test]# cat until.sh
#!/bin/bash
# using the until command
var1=100
until [ $var1 -eq 0 ]
do
echo $var1
var1=$[ $var1 - 25 ]
done
运行脚本:
[root@ceph01 test]# ./until.sh
100
75
50
25
五、嵌套循环
[root@ceph01 test]# cat double-for.sh
#!/bin/bash
# nesting for loops
for (( a = 1; a <= 3; a++ ))
do
echo "starting loop $a:"
for (( b = 1; b <= 3; b++ ))
do
echo " Inside loop: $b"
done
done
运行脚本:
[root@ceph01 test]# ./double-for.sh
starting loop 1:
Inside loop: 1
Inside loop: 2
Inside loop: 3
starting loop 2:
Inside loop: 1
Inside loop: 2
Inside loop: 3
starting loop 3:
Inside loop: 1
Inside loop: 2
Inside loop: 3
六、文件数据的循环
通常需要迭代存储的文件内部的项,可以通过以下两种实现:
使用嵌套循环
更改环境变量IFS
[root@ceph01 test]# cat file-data.sh
#!/bin/bash
# changing the IFS value
IFS.OLD=$IFS
IFS=$'\n'
for entry in 'cat /etc/passwd'
do
echo "Values in $entry -"
IFS=:
for value in $entry
do
echo " $value"
done
done
七、控制循环
(1)break命令
1》跳出单循环
[root@ceph01 test]# cat break.sh
#!/bin/bash
# breaking out of a for loop
for var1 in 1 2 3 4 5 6 7 8 9 10
do
if [ $var1 -eq 5 ]
then
break
fi
echo "Iteration number: $var1"
done
echo "The for loop is completed"
运行脚本:
[root@ceph01 test]# ./break.sh
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
2》跳出内循环
[root@ceph01 test]# cat break1.sh
#!/bin/bash
# breaking out of an inner loop
for (( a = 1; a < 4; a++ ))
do
echo "Outer loop: $a"
for (( b = 1; b < 100; b++ ))
do
if [ $b -eq 5 ]
then
break
fi
echo " Inner loop: $b"
done
done
运行脚本:
[root@ceph01 test]# ./break1.sh
Outer loop: 1
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
Outer loop: 2
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
Outer loop: 3
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
3》跳出外循环
[root@ceph01 test]# cat break2.sh
#!/bin/bash
# breaking out of an outer loop
for (( a = 1; a < 4; a++ ))
do
echo "Outer loop: $a"
for (( b =1; b < 100; b++ ))
do
if [ $b -gt 4 ]
then
break 2
fi
echo " Innter loop: $b"
done
done
运行脚本:
[root@ceph01 test]# ./break2.sh
Outer loop: 1
Innter loop: 1
Innter loop: 2
Innter loop: 3
Innter loop: 4
(2)continue命令
[root@ceph01 test]# cat continue.sh
#!/bin/bash
# using the continue command
for (( var1 = 1; var1 < 15; var1++ ))
do
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
then
continue
fi
echo "Iteration number: $var1"
done
运行脚本:
[root@ceph01 test]# ./continue.sh
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5
Iteration number: 10
Iteration number: 11
Iteration number: 12
Iteration number: 13
Iteration number: 14
八、处理循环的输出
[root@ceph01 test]# cat output.sh
#!/bin/bash
# redirecting the for output to a file
for (( a = 1; a < 10; a++ ))
do
echo "The number is $a"
done > test123.txt
echo "The command is finished."
运行脚本:
[root@ceph01 test]# ./output.sh
The command is finished.
[root@ceph01 test]# cat test123.txt
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9