shellwhile比较_[Shell] if、for、while流程语句以及整数字符串判断比较的实例详解...
前言:
實際上Shell是一個命令解釋器,它解釋由用戶輸入的命令并且把它們送到內核。不僅如此,Shell有自己的編程語言用于對命令的編輯,它允許用戶編寫由shell命令組成的程序。Shell編程語言具有普通編程語言的很多特點,比如它也有循環結構和分支控制結構等,用這種編程語言編寫的Shell程序與其他應用程序具有同樣的效果。
一,shell的流程語句
1,條件語句if else if
示例代碼:
[root@squid-2 script]# cat s1.sh
#!/bin/bash
echo "Please choose project:"
echo "1:zhu? 2:sha"
read project_no
if [ $project_no = "1" ];then
echo "111111"
elif [ $project_no = "2" ];then
echo "222222"
else echo "error"
fi
[root@squid-2 script]#
執行過程如下:
[root@squid-2 script]# sh s1.sh
Please choose project:
1:zhu?2:sha
1
111111
[root@squid-2 script]# sh s1.sh
Please choose project:
1:zhu?2:sha
2
222222
[root@squid-2 script]# sh s1.sh
Please choose project:
1:zhu?2:sha
3
error
[root@squid-2 script]#
2,for 循環
2.1 for i in
腳本如下:
[root@squid-2 script]# cat host_list.txt
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
[root@squid-2 script]#
測試執行結果:
[root@squid-2 script]# sh s21.sh
the host ip address is: 192.168.1.10
the host ip address is: 192.168.1.11
the host ip address is: 192.168.1.12
the host ip address is: 192.168.1.13
[root@squid-2 script]#
2.2 for((賦值;條件;運算語句))
腳本代碼:
[root@squid-2 script]# cat s22.sh
for((i=1;i<=10;i++));do
echo "the loop number i: $i";
done;
[root@squid-2 script]#
執行結果:
[root@squid-2 script]# sh s22.sh
the loop number i: 1
the loop number i: 2
the loop number i: 3
the loop number i: 4
the loop number i: 5
the loop number i: 6
the loop number i: 7
the loop number i: 8
the loop number i: 9
the loop number i: 10
[root@squid-2 script]#
3,while循環使用
條件語句結構:
while
do
action
done;
測試腳本:
[root@squid-2 script]# cat s3.sh
#!/bin/sh
i=10;
while [[ $i -gt 5 ]];do
echo"the loop number of while case is: $i";
((i--));
done;
[root@squid-2 script]
執行結果:
[root@squid-2 script]# sh s3.sh
the loop number of while case is: 10
the loop number of while case is: 9
the loop number of while case is: 8
the loop number of while case is: 7
the loop number of while case is: 6
[root@squid-2 script]#
4,until循環語句
示例腳本:
[root@squid-2 script]# cat s4.sh
#!/bin/sh
a=1;
until [[ $a -gt 6 ]];do
echo"the until number is: $a";
((a++));
done;
[root@squid-2 script]#
執行結果:
[root@squid-2 script]# sh s4.sh
the until number is: 1
the until number is: 2
the until number is: 3
the until number is: 4
the until number is: 5
the until number is: 6
[root@squid-2 script]#
5,shell選擇語句
5.1,使用case選擇語句使用(case/esac)
語法結構:
case $arg in
pattern | sample) # arg in pattern or sample
;;
pattern1) # arg in pattern1
;;
*) #default
;;
esac
說明:pattern1?是正則表達式,可以用下面字符:
*?????? 任意字串
??????? 任意字元
[abc]??a, b, 或c三字元其中之一
[a-n]?? 從a到n的任一字元
|?????? 多重選擇
代碼腳本:
[root@squid-2 script]# cat s51.sh
#!/bin/sh
case $1 in
start | begin)
echo "start something"
;;
stop | end)
echo "stop something"
;;
*)
echo "Ignorant"
;;
esac
[root@squid-2 script]#
PS:執行結果,這里需要帶入參數,參數值就在)前面的start、begin、stop、end之內,如果帶入別參數,則返回"Ignorant":
[root@squid-2 script]# sh s51.sh start
start something
[root@squid-2 script]# sh s51.sh begin
start something
[root@squid-2 script]# sh s51.sh end
stop something
[root@squid-2 script]# sh s51.sh stop
stop something
[root@squid-2 script]# sh s51.sh test1
Ignorant
5.2,select語句使用方法(產生菜單選擇)
語法結構:
select 變量name? in seq變量
do
action
done
代碼如下:
cat s52.sh
#!/bin/sh
select param in "begin""end" "exit"
do
case $param in
"begin")
echo "start something"
;;
"end")
echo "stop something"
;;
"exit")
echo "exit"
break;
;;
*)
echo "Ignorant"
;;
esac
done;
執行結果:
[root@squid-2 script]# sh s52.sh begin
1) begin
2) end
3) exit
#? 1
start something
#? 2
stop something
#? 3
exit
[root@squid-2 script]#
PS:執行的時候,只有輸入exit,才能退出來執行小窗口。
說明:select是循環選擇,一般與case語句使用。
二,shell語句的比較操作符
1,整數比較
規則說明:
-eq??????等于??????????? ????? if [ "$a" -eq "$b" ]
-ne??????不等于????????? ????? if [ "$a" -ne "$b" ]
-gt??????大于??????????? ????? if [ "$a" -gt "$b" ]
-ge??????大于等于??????? ????? if [ "$a" -ge "$b" ]
-lt??????小于??????????? ??????? if [ "$a" -lt "$b" ]
-le??????小于等于??????? ?????? if [ "$a" -le "$b" ]
<=???????小于等于(...)??????????????? (("$a" <= "$b" ))
>????????大于(...)??????????????????? (("$a" > "$b" ))
>=???????大于等于(...)??????????????? (("$a" >= "$b" ))
PS:小數據比較可使用AWK
示例代碼:
[root@squid-2 script]# cat com1.sh
a=$1
b=$2
if [ "$a" -eq "$b" ];then
echo"a = b true."
elif?[ ! "$a" -gt "$b" ];then
echo"a > b true."
else echo "a < b true."
fi
[root@squid-2 script]#
測試結果如下:
[root@squid-2 script]# sh com1.sh 1 1
a = b true.
[root@squid-2 script]# sh com1.sh 1 2
a > b true.
[root@squid-2 script]# sh com1.sh 1 0
a < b true.
[root@squid-2 script]#
2,字符串比較
2.1,規范以及使用
規則說明:
=?????等于?????????? if [ "$a"= "$b" ]
==????與=等價
!=????不等于???????? if [ "$a" ="$b" ]
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]???????? #需要對
>?????大于
-z????字符串為null,即長度為0
-n????字符串不為null,即長度不為0
示例代碼:
[root@squid-2 script]# cat com2.sh
a=$1
b=$2
# 1 the first method to implement
if [ "$a"x = "$b"x ];then
echo"a = b"
elif [ ! "$a"x? =?"$b"x ]; then
echo"a != b"
else echo "others"
fi
# 2 the second method to implement
if [ "$a"x == "$b"x ];then
echo "a = b"
elif [ "$a"x? !=?"$b"x ]; then
echo "a != b"
else echo "others"
fi
測試執行結果:
[root@squid-2 script]# sh com2.sh ccb aaa
a != b
a != b
[root@squid-2 script]# sh com2.sh ccb ccb
a = b
a = b
[root@squid-2 script]#
[root@squid-2 script]#
2.2,需要注意的地方
比較兩個字符串是否相等的辦法是:
if [ "$a"x = "b"x ];then
這里的關鍵有幾點:
1 使用單個等號
2 注意到等號兩邊各有一個空格:這是unix shell的要求
3 注意到"$a"x最后的x,這是特意安排的,因為當$test為空的時候,上面的表達式就變成了x = bx,顯然是不相等的。而如果沒有這個x,表達式就會報錯:[: =: unary operator expected
[[ $a == z* ]]?? #?如果$a以"z"開頭(模式匹配)那么將為true
[[ $a == "z*" ]] #?如果$a等于z*(字符匹配),那么結果為true
[ $a == z* ]???? # File globbing?和word splitting將會發生
[ "$a" == "z*" ] #?如果$a等于z*(字符匹配),那么結果為true
關于File globbing:
一點解釋,關于Fileglobbing是一種關于文件的速記法,比如"*.c"就是,再如~也是.
但是file globbing并不是嚴格的正則表達式,雖然絕大多數情況下結構比較像.
特殊字符的比較:
!= 不等于,如:if [ "$a" != "$b" ]
這個操作符將在[[]]結構中使用模式匹配.
< 小于,在ASCII字母順序下.如:
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
注意:在[]結構中"
> 大于,在ASCII字母順序下.如:
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
注意:在[]結構中">"需要被轉義.
-z 字符串為"null".就是長度為0.
-n 字符串不為"null"
注意:
使用-n在[]結構中測試必須要用""把變量引起來.使用一個未被""的字符串來使用! -z
或者就是未用""引用的字符串本身,放到[]結構中。雖然一般情況下可?以工作,但這是不安全的.習慣于使用""來測試字符串是一種好習慣。
2.3,判斷字符串是否空
示例代碼:
[root@squid-2 script]# cat com3.sh
#!/bin/bash
a=$1
if [ -z "$a" ]; then
echo"a is empty."
else echo "a is a object."
fi
[root@squid-2 script]
測試執行結果:
[root@squid-2 script]# sh com3.sh a
a is a object.
[root@squid-2 script]# sh com3.sh
a is empty.
[root@squid-2 script]#
3,文件判斷
規則說明:
-e?????????????????????????文件存在
-a?????????????????????????文件存在(已被棄用)
-f ? ? ? ? ? ? ? ? ? ? ?? ?被測文件是一個regular文件(正常文件,非目錄或設備)
-s?????????????????????????文件長度不為0
-d?????????????????????????被測對象是目錄
-b?????????????????????????被測對象是塊設備
-c?????????????????????????被測對象是字符設備
-p?????????????????????????被測對象是管道
-h?????????????????????????被測文件是符號連接
-L?????????????????????????被測文件是符號連接
-S(大寫) ? ? ? ? ? ? ?? ? ?被測文件是一個socket
-t?????????????????????????關聯到一個終端設備的文件描述符。用來檢測腳本的stdin[-t0]或[-t1]是一個終端
-r?????????????????????????文件具有讀權限,針對運行腳本的用戶
-w ? ? ? ? ? ? ? ? ? ? ?? ?文件具有寫權限,針對運行腳本的用戶
-x?????????????????????????文件具有執行權限,針對運行腳本的用戶
-u?????????????????????????set-user-id(suid)標志到文件,即普通用戶可以使用的root權限文件,通過chmod +s file實現
-k?????????????????????????設置粘貼位
-O ? ? ? ? ? ? ? ? ? ? ?? ?運行腳本的用戶是文件的所有者
-G ? ? ? ? ? ? ? ? ? ? ?? ?文件的group-id和運行腳本的用戶相同
-N ? ? ? ? ? ? ? ? ? ? ?? ?從文件最后被閱讀到現在,是否被修改
f1-nt f2 ? ? ? ? ? ? ? ? ??文件f1是否比f2新
f1 -ot f2 ? ? ? ? ? ? ? ? ? 文件f1是否比f2舊
f1 -ef f2 ? ? ? ? ? ? ? ? ? 文件f1和f2是否硬連接到同一個文件
示例腳本代碼:
[root@squid-2 script]# cat com4.sh
#!/bin/bash
a=$1
file=$2
if [ -d $a ]; then
echo"it is a directory."
elif [ -f "$a" ]; then
echo"it is a file."
else echo "no parameter."
fi
測試運行結果如下:
[root@squid-2 script]# sh com4.sh log1
it is a file.
[root@squid-2 script]# sh com4.sh old
it is a directory.
[root@squid-2 script]#
參考資料:
http://blog.csdn.net/yf210yf/article/category/1475895
總結
以上是生活随笔為你收集整理的shellwhile比较_[Shell] if、for、while流程语句以及整数字符串判断比较的实例详解...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MFC连接Access讲解(3合1)(T
- 下一篇: js下拉 selenium_seleni