歡迎您光臨本站 註冊首頁

bash shell筆記2 結構化命令一

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

二、使用結構化命令
知識內容:
# 改變命令流
# 使用if-then邏輯
# 嵌套if-then
# 測試條件
# 高級if-then功能
許多程序在腳本命令之間需要某些邏輯控制流,有些命令允許腳本根據變數值的條件或者命令的結果跳過一些命令或者循環執行這些命令,這叫做結構化命令.
1、使用if-then語句
最基本的結構化命令類型就是if-then語句,其格式如下:
if command
then
command
fi
意思是說:if語句後面的命令的退出狀態值是0,則執行then後面的所有命令;如果不是0則命令不執行.如下例子:
[root@wzp ~]# cat test2
#!/bin/bash
if date
then
echo "it works"
fi
[root@wzp ~]# ./test2
2011年 01月 28日 星期五 21:15:12 CST
it works
我們知道date命令是有效命令,查看$?肯定是0的,所以就執行then后的命令,這個應該不難理解.
2、if-then-else語句
這個語句可以提供一組命令,其格式如下:
if command
then
command
else
command
if
意思是說:if語句後面的命令的退出狀態值是0,則執行then後面的所有命令;跟if-then一樣,如果不是則執行else後面的命令.如下例子:
[root@wzp ~]# cat test2
#!/bin/bash
testuser=51cto
if grep $testuser /etc/passwd
then
echo the files for $testuser are:
ls -a /home/$testuser/.*
else
echo the user name $testuser does not exist !
fi
[root@wzp ~]# ./test2
the user name 51cto does not exist !
從上面可以看到由於不存在51cto用戶,則執行else後面的命令


但是當我創建了51cto用戶,則:
[root@wzp ~]# useradd 51cto
[root@wzp ~]# ./test2
51cto:x:502:502::/home/51cto:/bin/bash
the files for 51cto are:
/home/51cto/.bash_logout /home/51cto/.bashrc
/home/51cto/.bash_profile /home/51cto/.emacs

/home/51cto/.:
. .. .bash_logout .bash_profile .bashrc .emacs .mozilla

/home/51cto/..:
. .. 51cto aa bb lost found

/home/51cto/.mozilla:
. .. extensions plugins
直接執行then后的命令!這個也容易理解!~
3、嵌套if語句
有時需要在腳本代碼中檢查幾種情況,可以使用else的另一種版本叫elif,但是要嵌套許多if-then語句,後續有case命令介紹,這個更加的方便使用,這裡就不對嵌套if語句詳解了,其格式是:
if command
then
command
elif command
then
command
fi
4、test命令
這是一個重頭戲,bash shell提供一種在if-then語句中聲明test命令的另一種方法:
if [ condition ] //注意這裡的condition兩邊有空格!切記!
then
command
fi
test命令能夠評估以下3類命令:
# 數值比較
# 字元串比較
# 文件比較
下面就具體就test命令的用戶逐一說明!!!
4.1、數值比較
如下式數值比較表:
**********************************************
n1 -eq n2 檢查n1是不是等於n2
n1 -ge n2 檢查n1是不是大於或等於n2


n1 -gt n2 檢查n1是否大於n2
n1 -le n2 檢查n1是否小於或等於n2
n1 -lt n2 檢查n1是否小於n2
n1 -ne n2 檢查n1是否不等於n2
**********************************************
下面看看一個例子:
[root@wzp ~]# cat test2
#!/bin/bash
num1=10
num2=11
if [ $num1 -gt 5 ]
then
echo the test value $num1 is greater than 5
fi

if [ $num2 -eq $num1 ]
then
echo the values are equal
else
echo the values are different
fi
[root@wzp ~]# ./test2
the test value 10 is greater than 5
the values are different
給num1和num2賦值,然後通過測試,得出不同結論,這個好理解!
4.2、字元串比較
test命令允許對字元串值進行比較,主要分為如下三種比較類型:
1、字元串是否相同
2、字元串順序(大小)
3、字元串長度
也先看看test命令字元串比較表
********************************************
str1 = str2 檢查str1和str2是否相同
str1 != str2 檢查str1和str2是否不同
str1 < str2 檢查str1是否小於str2
str1 > str2 檢查str1是否大於str2
-n str1 檢查str1長度是否大於0
-z str1 檢查str1長度是否為0
********************************************
下面對三種類型舉個例子:
1)字元串是否相等
[root@wzp ~]# cat test2
#!/bin/bash
testuser=root
if [ $USER=$testuser ]
then
echo welcome $testuser
fi
[root@wzp ~]# ./test2


welcome root
可以看到字元串相等的顯示效果
[root@wzp ~]# cat test2
#!/bin/bash
testuser=root
if [ $USER!=$testuser ]
then
echo this is not $testuser
else
echo welcome $testuser
fi
[root@wzp ~]# ./test2
this is not root
不相等的情況則顯示then后的內容
2)字元串順序
[root@wzp ~]# cat test2
#!/bin/bash
str1=aaa
str2=bbb
if [ $str1 > $str2 ]
then
echo $str1 is greater than $str2
else
echo $str1 is less than $str2
fi
[root@wzp ~]# ./test2
aaa is greater than bbb
[root@wzp ~]# ll aaa
-rw-r--r-- 1 root root 0 01-28 22:22 aaa
在腳本中單獨使用了大於號>,雖然沒報錯,但結果是錯誤的,a打頭肯定是小於b的.這個腳本把大於號理解成輸出重定向,所以才出現這樣的情況,我們可以通過轉義大於號解決這個問題:
[root@wzp ~]# cat test2
#!/bin/bash
str1=aaa
str2=bbb
if [ $str1 > $str2 ] //此處添加一個就行了
then
echo $str1 is greater than $str2
else
echo $str1 is less than $str2
fi
[root@wzp ~]# ./test2
aaa is less than bbb
這樣就得出了正確的結果!
還有一點要說明的就是大小寫的問題,它的順序跟sort命令的順序是相反的!test命令是通過ASCII數值來決定排列順序的,這個稍微了解下就好.
3)字元串大小
評估一個變數是否包含數據,通過使用-n和-z比較很方便,如下例子:
[root@wzp ~]# cat test2
#!/bin/bash
str1=aaa
str2=


if [ -n $str1 ] //長度是否大於0
then
echo the string $str1 is not empty
else
echo the string $str1 is empty
fi
if [ -z $str2 ] //長度是否為0
then
echo the string $str2 is empty
else
echo the string $str2 is not empty
fi
if [ -z $str3 ] //長度是否為0
then
echo the string $str3 is empty
else
echo the string $str3 is not empty
fi
[root@wzp ~]# ./test2
the string aaa is not empty
the string is empty
the string is empty
如上對於str1和str2應該沒什麼問題,而對於沒有定義變數str3則認定其字元串為零.
4.3、文件比較
test命令能夠檢測linux文件系統上的文件狀態和路徑,對於文件比較這一塊挺多東西的,下面一一道來,看看test命令文件比較表:
****************************************************************
-d file 檢測file是否存在並且是一個目錄
-e file 檢測file是否存在
-f file 檢測file是否存在並且是一個文件
-r file 檢測file是否存在並且刻度
-s file 檢測file是否存在並且不為空
-w file 檢測file是否存在並且可寫
-x file 檢測file是否存在並且可執行
-O file 檢測file是否存在並且被當前用戶擁有
-G file 檢測file是否存在並且默認組是否為當前用戶組
file1 -nt file2 檢測file1是否比file2新
file1 -ot file2 檢測file1是否比file2舊
*****************************************************************
對於上面這個表,如下通過10個例子說明:
1)檢測目錄
[root@wzp ~]# cat test2
#!/bin/bash
if [ -d $HOME ]


then
echo your home directory exists
cd $HOME
ls
else
echo there is someting wrong with your HD
fi
[root@wzp ~]# ./test2
your home directory exists
aaa bbb install.log mbox test2
anaconda-ks.cfg Desktop install.log.syslog one
使用-d把我這個root的家目錄ls出來了~~
2)檢測對象是否存在
[root@wzp ~]# cat test2
#!/bin/bash
if [ -e $HOME ]
then
echo yes, the HD exists
if [ -e $HOME/bbs.51cto ]
then
echo the bbs.51cto exists
else
touch $HOME/bbs.51cto
echo creating new file name bbs.51cto
fi
fi
從上面我們看到目錄肯定是存在的,我的root嘛,但是這個bbs.51cto是不存在的,所以第一次執行文件顯示不存在,並且私下創建了.
[root@wzp ~]# ./test2
yes, the HD exists
creating new file name bbs.51cto
第二次執行文件那肯定是顯示存在bbs.51cto了,呵呵
[root@wzp ~]# ./test2
yes, the HD exists
the bbs.51cto exists
3)檢測文件
-e適合用於檢測文件和目錄,要確定對象是文件,使用-f比較,下例:
[root@wzp ~]# cat test2
#!/bin/bash
if [ -e $HOME ]
then
echo the $HOME exists !
if [ -f $HOME ]
then
echo yes, it is a file
else
echo no, it is not a file
if [ -f $HOME/.bash_history ]
then
echo but $HOME/.bash_history is a file
else
echo it is not a file too
fi
fi
else
echo there is not object!


fi
[root@wzp ~]# ./test2
the /root exists !
no, it is not a file
but /root/.bash_history is a file
上面的例子應該很好懂,注意-f是檢測文件,-e可以檢測文件和目錄就行了!
4)檢測是否可讀
通過-r可檢測可讀性,如下例:
[51cto@wzp ~]$ whoami
51cto
[51cto@wzp ~]$ cat test
#!/bin/bash
testfile=/etc/shadow
if [ -f $testfile ]
then
if [ -r $testfile ]
then
ls -l $testfile
else
echo "i'm unable to read the file"
fi
else
echo "the file doesn't exist"
fi
[51cto@wzp ~]$ sh test
i'm unable to read the file
[51cto@wzp ~]$ su - root
口令:
[root@wzp ~]# sh /home/51cto/test
-r-------- 1 root root 1204 01-28 21:27 /etc/shadow
如上可知普通用戶51cto無法讀取文件.
5)檢測空文件
通過-s檢測文件是否為空,例子如下:
[root@wzp ~]# cat test2
#!/bin/bash
file=testfile
touch $file
if [ -s $file ]
then
echo the file exists and has data in it
else
echo the file exists but empty
fi
date > $file
if [ -s $file ]
then
echo the file exists and has data in it
else
echo the file exists but empty
fi
[root@wzp ~]# ./test2
the file exists and has data in it
the file exists and has data in it
先是創建空文件,然後通過重定向date進去判斷文件不為空,這個好理解!
6)檢測是否能夠可寫
通過-w檢測文件是否可寫,例子如下:
[root@wzp ~]# cat test2
#!/bin/bash


file=$HOME/testfile
touch $file
chmod u-w $file
now='date %y%m%d-%H%M'
if [ -w $file ]
then
echo the file could be written
else
echo "the file couldn't be written"
fi
chmod u w $file
if [ -w $file ]
then
echo the file could be written
$now > $HOME/testfile
echo and the file views $file
else
echo "the file couldn't be written"
fi
[root@wzp ~]# ./test2
the file could be written
the file could be written
and the file views /root/testfile
[root@wzp ~]# cat /root/testfile
110129-1317
上面的例子相等好理解,沒什麼好說的.
7)檢測是否能運行文件
通過-x可以檢測文件是否可被執行,如下例:
[root@wzp ~]# cat test2
#!/bin/bash
file=$HOME/testfile2
touch $file
chmod u x $file
if [ -x $file ]
then
echo the file could be run
else
echo "the file couldn't be run"
fi
[root@wzp ~]# ./test2
the file could be run
[root@wzp ~]# ll testfile2
-rwxr--r-- 1 root root 0 01-29 13:21 testfile2
這個也沒什麼疑惑之處.
8)檢測所有者
通過-O可以檢測你是否屬於這個文件的所有者,如下例:
[root@wzp ~]# cat test2
#!/bin/bash
file=/etc/passwd
if [ -O $file ]
then
echo "you are the owner of the $file"
else
echo "you aren't the owner of the $file"
fi
[root@wzp ~]# ./test2
you are the owner of the /etc/passwd
[root@wzp ~]# ll /etc/passwd
-rw-r--r-- 1 root root 1877 01-28 21:27 /etc/passwd
/etc/passwd屬有者肯定是root啦!


9)檢測所屬組
-G檢測文件的默認組,如果它跟當前用戶的默認組匹配,則檢測成功,如下例:
[root@wzp ~]# cat test2
#!/bin/bash
file=/etc/passwd
if [ -G $file ]
then
echo "you are in the same group as $file"
else
echo "you aren't in the same group as the $file"
fi
[root@wzp ~]# ./test2
you are in the same group as /etc/passwd
[root@wzp ~]# ll -d /etc/passwd
-rw-r--r-- 1 root root 1877 01-28 21:27 /etc/passwd
很明顯,/etc/passwd肯定屬於root組咯
[root@wzp ~]# chgrp 51cto /etc/passwd
[root@wzp ~]# ll -d /etc/passwd
-rw-r--r-- 1 root 51cto 1877 01-28 21:27 /etc/passwd
[root@wzp ~]# ./test2
you aren't in the same group as the /etc/passwd
當把這個文件的所屬組改成51cto后,則檢測不成功了,不過現實中別做這樣的修改
10)檢測文件日期
一般通過-nt和-ot來比較兩個文件之間的新舊,這裡指的是創建或修改日期,例子:
[root@wzp ~]# ll aa bb
-rw-r--r-- 1 root root 0 01-29 13:41 aa
-rw-r--r-- 1 root root 0 01-29 14:23 bb
aa
[root@wzp ~]# cat test2
#!/bin/bash
if [ $HOME/aa -nt $HOME/bb ]
then
echo the aa is newer than bb
else
echo the aa is older than bb
fi
[root@wzp ~]# ./test2
the aa is older than bb
下面我把aa文件修改下內容:
[root@wzp ~]# vim aa
[root@wzp ~]# ll aa bb
-rw-r--r-- 1 root root 3 01-29 14:28 aa
-rw-r--r-- 1 root root 0 01-29 14:23 bb
[root@wzp ~]# ./test2
the aa is newer than bb
這個結果應該很容易接受!


OKOK,到這裡test命令的內容就結束了,如上各種類型比較得以說明了.
5、複合條件檢測
if-then語句可以使用布爾邏輯來合併檢測條件,格式:
*****************************************************
[ condition ] && [ condition ] 這個表示邏輯與 and
[ condition ] || [ condition ] 這個表示邏輯或 or
*****************************************************
如下例子:
[root@wzp ~]# cat test2
#!/bin/bash
if [ -d $HOME ] && [ -w $HOME/wzp ]
then
echo the file exists and you can wirte $HOME/wzp
else
echo "you can't write the file"
fi
if [ -d $HOME ] || [ -w $HOME/wzp ]
then
echo the file exists and you can wirte $HOME/wzp
else
echo "you can't write the file"
fi
[root@wzp ~]# ./test2
you can't write the file
the file exists and you can wirte /root/wzp
[root@wzp ~]# ll /root/wzp
ls: /root/wzp: 沒有那個文件或目錄
$HOME是root家目錄,肯定存在的,但是不存在文件/root/wzp
6、if-then的高級特徵
既然說是高級特徵,必然作用非凡了,呵呵
其功能體現是通過雙圓括弧(())表示數學表達式和雙方括弧[[]]表示高級字元串處理函數.
6.1、使用雙圓括弧
先看一個例子:
[root@wzp ~]# cat test2
#!/bin/bash
num1=10
if (( $num1 ** 2 > 90 ))
then
(( num2=$num1 ** 2 ))
echo the square of $num1 is $num2
fi
[root@wzp ~]# ./test2
the square of 10 is 100
這裡的**表示取冪,由此可見(())可以很方便處理數學表達式
6.2、使用雙方括弧


使用雙方括弧可以定義與字元串值相匹配的正則表達式,如例:
[root@wzp ~]# cat test2
#!/bin/bash
if [[ $USER == r* ]]
then
echo hello $USER
else
echo sorry , i "don't" know you
fi
[root@wzp ~]# ./test2
hello root
[root@wzp ~]# cp test2 /home/51cto/
[root@wzp ~]# su - 51cto
[51cto@wzp ~]$ sh test2
sorry , i don't know you
這裡用到了通配符*,表示r開頭的任何用戶,正則表達式的內容放置後面說明!
[root@wzp ~]# useradd rrr
[root@wzp ~]# cp test2 /home/rrr
[root@wzp ~]# su - rrr
[rrr@wzp ~]$ sh test2
hello rrr
這都好理解吧~~~
7、case命令
還記得前面提及到case命令吧,當在一組可能的值中尋找特定的值,可以寫if-then-else語句,其中嵌套elif語句繼續著if-then檢測,這樣子就很冗長麻煩.所以可以通過case命令簡化,以列表導向格式檢測單個變數的多個值,格式為:
case xxx in
xx | xx) command;
xx) command;
*) default command;
esac
下面通過一個例子:
[root@wzp ~]# cat test2
#!/bin/bash
case $USER in
root | testuser)
echo welcome $USER
echo your are admin;;
51cto)
echo welcome $USER;;
*)
echo welcome $USER;;
esac
[root@wzp ~]# ./test2
welcome root
your are admin
[root@wzp ~]# cp test2 /home/51cto/
cp:是否覆蓋“/home/51cto/test2”? y
[root@wzp ~]# su - 51cto
[51cto@wzp ~]$ sh test2
welcome 51cto
判斷是root和51cto用戶則顯示特定內容,如果通過其他用戶執行,如下:

[root@wzp ~]# useradd testuser24
[root@wzp ~]# cp test2 /home/testuser24/
[root@wzp ~]# su - testuser24
[testuser24@wzp ~]$ sh test2
welcome testuser24
這個都比較好理解吧,假設通過存在的用戶testuser執行,那顯示的內容肯定是:
welcome testuser
your are admin
哈哈,說是admin也不正確了.

好了,對於部分結構化命令就說到這裡,後續是循環、迭代等結構化命令的講述.

本文出自 「twenty_four」 博客,請務必保留此出處http://twentyfour.blog.51cto.com/945260/505654


[火星人 ] bash shell筆記2 結構化命令一已經有594次圍觀

http://coctec.com/docs/linux/show-post-49257.html