Linux---shell編程總結之if語句、case語句(二)

1、if-then語句格式:

if command
then
	command
fi
----------------------------------------------------------------
if pwd
then
	echo "It worked"
fi

還可以將then寫到一行:

if command ; then
	command
fi

2、if-then-else語句格式:

if command
then
	command
else
	command
fi
----------------------------------------------------------------
testuser=Christine
if grep $testuser /etc/passwd
then
	echo "The bash files for user $testuser are:"
	ls -a /home/$testuser/.b*
	echo
else
	echo "The user $testuser dose not exit on this system."
	echo
fi

3、嵌套if格式:

if command
then
	command
else
	command
	if command
	then
		command
	fi
fi

4、多重if判斷格式:

if command1
then
	commands
elif command2
then
	commands
fi
----------------------------------------------------------------
testuser=Christine
if grep $testuser /etc/passwd
then
	echo "The user $testuser exists on this system."
#	
elif ls -d /home/$testuser/
then
	echo "The user $testuser dose not exist on this system."
	echo "However, $testuser has a directory."
else
	echo "The user $testuser dose not exist no this system."
	echo "And, $testuser does not have a directory."
fi

5、if測試比較條件格式:

if [ condition]
then
	commands
fi

if測試比較條件分類:數值比較、字符串比較、文件比較。
數字比較:
n1 -eq n2:n1 = n2
n1 -lt n2:n1 < n2
n1 -le n2:n1 <= n2
n1 -gt n2:n1 > n2
n1 -ge n2:n1 >= n2
n1 -ne n2:n1 != n2
例子:

value1=10
value2=11
#
if [ $value1 -gt 5 ]
then
	echo "The test value $value1 is greater than 5"
fi
#
if [ $value1 -eq $value2 ]
then
	echo "The values are equal"
else
	echo "The values are different"
fi

字符串比較:
str1 = str2:檢查str1和str2是否相同
str1 !=str2:檢查str1和str2是否不同
str1 < str2:檢查str1是否比str2小
str1 > str2:檢查str1是否比str2大
-n str1:檢查str1不爲0
-z str2:檢查str2是否爲0
例子:

val1=Testing
val2=testing
#
if [ $val1 \> $val2 ]
then
	echo "$val1 is greater than $val2"
else
	echo "$val1 is less than $val2"
fi

文件比較:
-d file:檢查file是否存在並且是個目錄
-e file:檢查file是否存在
-f file:檢查file是否存在並且是個文件
-r file:檢查file是否存在並且可讀
-s file:檢查文件是否在並且不爲空
-w file:檢查file是否存在並且可寫
-x file:檢查file是否存在並且可執行
-O file:檢查file是否存在並且屬於當前用戶
-G file:檢查file是否存在並且默認組與當前用戶組相同
file1 -nt file2:file1是否比file2新
file1 -ot file2:file1是否比file2舊
例子:

jump_directory=/home/arthur
#
if [ -d $jump_directory ]
then
	echo "The $jump_directory directory exists"
	cd $jump_directory
	ls
else
	echo "The $jump_directory directory does not exists"
fi

6、邏輯條件測試:
[ condition1 ] && [ condition2 ]:相當於and
[ condition1 ] || [ condition2 ]:相當於or
7、使用雙括號:提供了更多的數學符號,在這裏面運算符不需要轉義
(( expression ))
例子:

val1=10
#
if (( $val1 ** 2 > 90 ))
then
	(( val2 = $val1 ** 2 ))
	echo "The square of $val1 is $val2"
fi

8、使用雙方括號:提供了模式匹配
[[ expression ]]
例子:

if [[ $USER == r* ]] 
then
	echo "Hello $USER"
else
	echo "Sorry, I do not know you"
fi

9、case命令結構:

case var in
a)
commands
;;
b)
commands
;;
esac

例子:

case $USER in
rich | barbara)
	echo "Welcome, $USER"
	echo "Please enjoy your visit"
;;
testing)
	echo "Special testing account"
;;
jessica)
	echo "Do not forget to log off when you're done"
;;
*)
	echo "Sorry, you are not allowed here"
;;
esac

發佈了8 篇原創文章 · 獲贊 1 · 訪問量 295
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章