Take a few simple examples of the judgments involved
1. Comparison of file-related judgments
#!/bin/Bashif[$#-lt1 ] Then Echo "No file name"Exit1elif[-D $1 ] Then Echo$1is directoryelif[-B $1 ] Then Echo$1is block deviceelif[-H $1 ] Then Echo$1Is linkfileelif[-C $1 ] Then Echo$1is character deviceelif[-F $1 ] Then Echo$1is normalfileElse Echo "Don ' t recognize $"fiif[-R $1 ] Then Echo$1Is can readfiif[ -W$1 ] Then Echo$1IS canWritefiif[-X $1 ] Then Echo$1Is can excutefiif[-U $1 ] Then Echo$1Has SUID attributionfiif[-G $1 ] Then Echo$1Has SGID attributionfiif[-K $1 ] Then Echo$1Has Sticky bit attributionfiif[-S $1 ] Then Echo$1is a XXXfiExit0
The results of the implementation are as follows:
2. Number-related judgments
A.
#!/bin/Bashif[" $"-eq" $"]; Then# if[" $"-ne" $"]; Then Echo$1Equal $2fiif[" $"-lt" $"]; Then# if[" $"-le" $"]; Then Echo$1 Lessthan $2fiif[" $"-gt" $"]; Then# if[" $"-ge" $"]; Then Echo$1Grate Then$2fi
B.
#!/bin/Bashread-P"Input The Age:" Age Case$ageinch [0-9]| [1][0-2]) Echo " Child" ;; [1][0-9]) Echo " Young" ;; [2-5][0-9] ) Echo "Adult" ;; [6-9][0-9]) Echo " Old" ;; * ) Echo "Not people" ;;EsacExit0
The results of the implementation are as follows:
B.
#!/bin/Bash
Read-P"Iuput your Birthday (mmdd.ex> 0709):"Birnow=`Date+%m%d '
if["$bir"=="$now"]; Then Echo "Happy Birthday!!!"elif["$bir"-gt"$now"]; Then Year=`Date+%Y ' Total_d=$(($((`Date--Date="$year $bir"+%s '-'Date+%s '))/ -/ -/ -))Echo "Your birthday'll be $total _d later"Else Year=$((`Date+%y ' +1)) Total_d=$(($((`Date--Date="$year $bir"+%s '-'Date+%s '))/ -/ -/ -))Echo "Your birthday'll be $total _d later"fi
The results of the implementation are as follows:
3. String comparisons
#!/bin/Bashif[" $"==" $"]; Then# if[[" $"==" $"]]; Then Echo$1Equal $2fiif[" $"!=" $"]; Then Echo$1No equal $2fiif[[" $">" $"]]; Then# if[" $"\>" $"]; Then Echo$1Grate Then$2fiif[[" $"<" $"]]; Then# if[" $"\<" $"]; Then Echo$1 Less Then$2fiif[-Z" $"]; Then Echo$1is emptyfiif[-N" $"]; Then Echo$1is no emptyfiif[[" $"==1* ]]; Then Echo$1=="1*" 1111111111fiif[[" $"=="1*"]]; Then Echo$1== \"1*\ "2222222222fi
The results of the implementation are as follows:
Two times comparison of 1*, pattern matching and full string matching, respectively
Shell simple Use (-) judgment