Instance one, compare two integer sizes
#!/bin/Bash while true DoRead-P"Please input the int nums:"a BExpr$a + $b +0>/dev/NULL 2>&1 #判断是否为整数 if[$?-eq0];Then #返回值是否为0 if[$a-gt $b]; Then Echo "$a > $b"Exit0 elif[$a-lt $b]; Then Echo "$a < $b"Exit0 Else[$a-eq $b]Echo "$a = $b"Exit0 fi fi Done
Example two, test statement and if condition sentence mixed use, through directory installation LAMP,LNMP
directory Installation: #!/bin/SHPath=/server/scrips[! -D"$path"] &&mkdir$path-P#menuCat<<END1. [InstallLamp]2. [InstallLnmp]3. [Exit] pls input the num you wang:endread numExpr$num +1&>/dev/NULLif[$?-ne0]; Then Echo "The num You input must is {1|2|3}"Exit1fi[$num-eq1]&&{ Echo "start install lamp." Sleep 2; [ -X"$path/lamp.sh"]||{ Echo "does not exist or can is not exec."Exit1} $path/lamp.SH#source $path/lamp.SHExit $?}if[$num-eq2]; Then Echo "start install LNMP." Sleep 2; if[-X"$path/lamp.sh"]; Then$path/lnmp.SH#source $path/lamp.SHExit $?Else Echo "does not exist or can is not exec."Exitfifiif[$num-eq3]; Then EchoBye Exit3fi[[ ! $num =~[1-3]] &&{ Echo "The num You input must is {1|2|3}" Echo "Input ERROR"Exit4}
Shell Exercise 1 Writing shell conditional sentence exercises