shell腳本編程----使用結構化命令(二)

一、使用if—then語句

if--then語句的格式如下:

if command
then
    commands
fi

例子:

[root@ceph01 test]# cat if-then.sh 
#!/bin/bash
# testing multiple commands in the then section
testuser=root
if grep $testuser /etc/passwd
then
	echo The bash files for user $testuser are:
	ls -a /$testuser/.b*
fi

運行腳本

[root@ceph01 test]# ./if-then.sh 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
The bash files for user root are:
/root/.bash_history  /root/.bash_profile
/root/.bash_logout   /root/.bashrc

二、if—then—else語句

if-then-else語句的命令格式:

if command
then
    command
else
    command
fi

例子:

[root@ceph01 test]# cat if-then-else.sh 
#!/bin/bash
# testing the else section
testuser=badtest
if grep $testuser /etc/passwd
then
	echo The files for user $testuser are:
	ls -a /$testuser/.b*
else
	echo "The user name $testuser doesn't exist on this system"
fi

運行腳本:

[root@ceph01 test]# ./if-then-else.sh 
The user name badtest doesn't exist on this system

三、嵌套if語句

有時需要在腳本代碼中檢查幾種情況,這時可以不必編寫單獨的if-then語句,可以使用else部分的另一版本,稱爲elif

elif以另一個if-then語句繼續else部分:

if    command1
then 
     commends
elif   command2
then 
     more   commands
fi

 

也可以把多個elif語句串在一起,創建一個大的if-then-elif組:

if command1
then 
    command set 1
elif command2
then
    command set 2
elif command3
then 
    command set 3
elif command4
then
    command set 4
fi

 

四、test命令

       test命令提供一種檢測if-then語句中不同條件的的方法。如果test命令中列出的條件評估值爲true,test命令以0退出狀態代碼退出,這使if-then語句使用與其他編程語言中的if-then語句一樣的方式運行。如果條件爲false,則test命令退出,使得if-then語句失敗。

test命令的格式:

test condition
if  test  condition
then 
       commands
fi

test命令提供一種檢測if-then語句,test評估以下三類:數值比較;字符串比較;文件比較。

1>數值比較:

[root@ceph01 test]# cat test-value.sh 
#!/bin/bash
#using numeric test comparisons
val1=10
val2=11
if [ $val1 -gt 5 ]           #測試val1是否大於5
then
	echo "The test value $val1 is greater than 5"
fi

if [ $val1 -eq $val2 ]       #測試val1和val2是否相等
then 
	echo "The values are equal"
else
	echo "The values are different"
fi 

 

 

 

運行腳本:

[root@ceph01 test]# ./test-value.sh 
The test value 10 is greater than 5
The values are different

2>字符串比較:

字符串相等

[root@ceph01 test]# cat test-string.sh 
#!/bin/bash
#testing string equality
testuser=root

if [ $USER != $testuser ]
then
	echo "This isn't $testuser"
else
	echo "Welcome $testuser"
fi

運行腳本:

[root@ceph01 test]# ./test-string.sh 
Welcome root

字符串順序:

大於和小宇符號一定要轉義,否則shell會當做重定向符號;

大於和小於順序與在sort命令中的順序不同

[root@ceph01 test]# cat test-string-order.sh 
#!/bin/bash
#mis-using string comparisons

val1=bashball
val2=hockey

if [ $val1 > $val2 ]
then
	echo "$val1 is greater than $val2"
else
	echo "$val1 is less than $val2"
fi

運行腳本:

[root@ceph01 test]# ./test-string-order.sh 
bashball is greater than hockey

字符串大小

[root@ceph01 test]# cat test-string-length.sh 
#!/bin/bash
#testing string length

val1=testing
val2=''

if [ -n $val1 ]
then
	echo "Test string '$val1' is not empty"
else
	echo "The string '$val1' is empty"
fi

if [ -z $val2 ]
then
	echo "The string '$val2' is empty"
else
	echo "The string '$val2' is not empty"
fi

if [ -z $val3 ]
then
	echo "The string '$val3' is empty"
else
	echo "The string '$val3' is not empty"
fi

運行腳本:

[root@ceph01 test]# ./test-string-length.sh 
Test string 'testing' is not empty
The string '' is empty
The string '' is empty

3>文件比較

檢查目錄

[root@ceph01 test]# cat test-file-leap.sh 
#!/bin/bash
# look before you leap

if [ -d $HOME ]
then
	echo "Your HOME directory exists"
	cd $HOME
	ls -a
else
	echo "There's a problem with your HOME directory"
fi

運行腳本:

[root@ceph01 test]# ./test-file-leap.sh 
Your HOME directory exists
.				 .cshrc
..				 fio-ceph.sh
anaconda-ks.cfg			 force-eth0-100Mbps.sh
.ansible			 lvm-resize-sda.sh
.bash_history			 .pki
.bash_logout			 .ssh
.bash_profile			 .tcshrc
.bashrc				 updates
.cache				 .viminfo
CentOS-7-aarch64-Everything.iso  .Xauthority
.config

檢查對象是否存在

[root@ceph01 test]# cat test-file-checking.sh 
#!/bin/bash
# checking if a directory exists

if [ -e $HOME ]
then
	echo "OK on the directory,now let's check the file"
	#checking if a file exists
	if [ -e $HOME/testing ]
	then
		#the file exists,append data to it
		echo "Appending date to existing file"
		date >> $HOME/testing
	else
		#the file doesn't exist,create a new file
		echo "Creating new file"
		date > $HOME/testing
	fi
else
	echo "Sorry,you don't have a HOME directory"
fi

運行腳本:

[root@ceph01 test]# ./test-file-checking.sh 
OK on the directory,now let's check the file
Creating new file

[root@ceph01 test]# ./test-file-checking.sh 
OK on the directory,now let's check the file
Appending date to existing file

檢查文件

[root@ceph01 test]# cat test-file-check.sh 
#!/bin/bash
# check if a file

if [ -e $HOME ]
then
	echo "The object exists,is it a file"
	if [ -f $HOME ]
	then
		echo "Yes,it's a file!"
	else
		echo "No,it's not a file!"
		if [ -f $HOME/.bash_history ]
		then
			echo "But this is a file!"
		fi
	fi
else
	echo "Sorry,the object doesn't exist"
fi

運行腳本:

[root@ceph01 test]# ./test-file-check.sh 
The object exists,is it a file
No,it's not a file!
But this is a file!

是否能讀

[root@ceph01 test]# cat test-file-read.sh 
#!/bin/bash
#testing if you can read a file 
pwfile=/etc/shadow

#first,test if the file exists,and is a file
if [ -f $pwfile ]
then
	#new test if you can read it
	if [ -r $pwfile ]
	then
		tail $pwfile
	else
		echo "Sorry,I'm unable to read the $pwfile file"
	fi
else
	echo "Sorry,the file $file doesn't exist"
fi

 

運行腳本:

[root@ceph01 test]# ./test-file-read.sh 
gnome-initial-setup:!!:17530::::::
rpcuser:!!:17530::::::
nfsnobody:!!:17530::::::
avahi:!!:17530::::::
postfix:!!:17530::::::
sshd:!!:17530::::::
ntp:!!:17530::::::
oprofile:!!:17530::::::
tcpdump:!!:17530::::::
ceph:!!:17885::::::

檢查空文件

[root@ceph01 test]# cat test-file-null.sh 
#!/bin/bash
#testing if a file is empty
file=t15test
touch $file

if [ -s $file ]
then
	echo "The $file file exists and has data in it"
else
	echo "The $file exists and is empty"
fi

date > $file
if [ -s $file ]
then
	echo "The $file file has data in it "
else
	echo "The $file is still empty"
fi

運行腳本

[root@ceph01 test]# ./test-file-null.sh 
The t15test exists and is empty
The t15test file has data in it 

檢查是否能夠向文件中寫入數據

 

檢查是否能夠運行文件

檢查所有權

檢查文件日期

 

 

 

 

5、複合條件檢查(布爾操作符:&&    ||)

&&:AND布爾操作符(兩者都成立纔會執行then)

||   :OR布爾操作符(兩者滿足其一就會執行then)

$:cat  test

#!/bin/bash

#testing  compound comparisons

if  [-d  $HOME]  &&  [-w  $HOME/testing]

then

       echo  "The  file  exits  and you  can write to it "

else 

      echo  "I can't  write to the file"

fi

6、if-then的高級特徵

雙圓括號表示數學表達式;雙方括號表示高級字符串處理函數。

雙圓括號:

$:cat  test

#!/bin/bash

#using  double  parenthesis

val1=10

if ( ($val1 ** 2) > 90)

then

    ( (val2 = $val1 ** 2) )

    echo  "The  square of  $vall is $val2"

fi

雙方括號:雙方括號命令爲字符串比較提供高級功能,格式爲:[ [ expression ] ]

$:cat  test

#!/bin/bash

#using  pattern  matching

if  [ [ $USER == r* ] ]

then

      echo  "hello  $USER"

else

     echo  "Sorry ,  I  don't  know   you"

fi

7、case命令

case命令將指定的變量與不同的模式進行比較。

case   Varisble  in

pattern1  |  pattern2)  commands1;

pattern3)   commands2;;

*)  default  commands;;

esac

 

$:cat  test

#!/bin/bash

#using the case command 

case  $USER in

rich  |  barbara)

     echo  "Welcome,$USER"

     echo "Please  enjoy  your visit";;

testing)

     echo  "Special testing account";;

jessica)

    echo  "Don't  forget  to  log  off  when  you're  done";;

*)

    echo  "Sorry,  you're not  allowed  here";;

esac

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