歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Linux shell編程----shell中的判斷

Linux shell編程----shell中的判斷

日期:2017/3/1 14:43:30   编辑:SHELL編程
Linux shell編程----shell中的判斷 1 test判斷 1 test -f $文件名 判斷文件是否存在 test -f $文件名 舉例:通過test -f命令去查找/home下是否存在input.txt,如果有輸出exist,否則輸出no exist 2 其它還有很多的命令 1 test -d $目錄名 判斷目錄是否存在 2 test -r $文件名 判斷文件是否有讀權限 3 test -w $文件名 判斷文件是否有寫權限 4 test -x $文件名 判斷文件是否有執行權限 2 [[判斷 1 在中括號中必須使用空格來分割 比如[ '10' < '20' ]這種判斷,中括號裡面最好要有4個空格 2 在中括號中變量,最好都是要以雙引號括起來 a="this" b="this is" 比如[ "$a" != "$b" ] 3 在中括號中的常數,最好都以單引號括起來 比如[ '4' == '4' ] 3 條件判斷(以下只是以[]作為舉例,也可以是test,只是[]看起來比較美觀) 1 單分支判斷 結構 if []; then statement fi 2 雙分支判斷 結構 if []; then statement else statement fi 3 多分支判斷 結構 if []; then statement elif []; then statement else statement fi 4 case實現多分支判斷 結構 case $Variable in 條件1) statement;; 條件2) statement;; 條件3) statement;; ............................. *) statement;; esac
Copyright © Linux教程網 All Rights Reserved