Shell腳本編程---使用結構化命令(三)

一、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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章