I. Logical judgments in the shell
- Format 1:if condition; Then statement; Fi
a=2if [ $a -ge 2 ];then echo ">=2";fi
- Format 2:if condition; Then statement; else statement; Fi
a=1if [ $a -ge 2 ];then echo ">=2";else echo "<2";fi
- Format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi
a=6if [ $a -ge 8 ] ;then echo ">=8";elif [ $a -eq 6 ] ;then echo "=6" ;else echo "<6" ;fi
Logical judgment expression: if [$a-gt $b]; If [$a-lt 5]; If [$b-eq 10] et-gt (>); -lt (<); -ge (>=); -le (<=);-eq (= =); -ne (! =) Note that there are spaces everywhere
- can use && (and) | | (or) combine multiple conditions
if [ $a -gt 5 ] && [ $a -lt 10 ]; then echo "good" ; fiif [ $b -gt 5 ] || [ $b -lt 3 ]; then echo "no" ; fi
Second, if determine file, directory properties
[-F file] Determines if it is a normal file, and there is
[-D file] Determines if it is a directory and exists
[-E file] to determine whether files or directories exist
[-R File] to determine if the document is readable
[-W file] Determines whether the file is writable
[-X file] Determines whether the file is executable
[!-e file] to determine whether a file or directory does not exist
Iii. some special usages of if judgment
If [-Z "$a"]?? This indicates what happens when the value of variable A is empty
If [-n ' $a '] means that the value of variable A is not empty
If Grep-q ' 123 ' 1.txt; Then?? What happens if the 1.txt contains a ' 123 ' row
if [!-e file]; Then what happens when the file doesn't exist?
if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ...
Symbols such as <,>,==,!=,>=,<= cannot be used in []
Iv. case judgment in the shell
Format case?? Variable name in?? ?? ?? ?? ??
?? ? value1)?? ?? ?? ?? ?? ?
? ?? ??? Command??? ?? ?? ?? ?? ???
;; ?? ??
?? ? value2)
?? ?? ?? Command??? ?? ?? ?? ?? ?? ???
;;
? ?? ?? *)? ?? ?
Commond?? ?? ?? ?? ?? ?? ?
;;?? ?? ?? ?? ?? ?? ?
Esac
In a case program, you can use a |, meaning, or mean in a condition, such as? 2|3)?? Command?;;
case example
#!/bin/bashread -p "Please input a number: " nif [ -z "$n" ]then echo "Please input a number." exit 1fin1=`echo $n|sed ‘s/[0-9]//g‘`if [ -n "$n1" ]then echo "Please input a number." exit 1fiif [ $n -lt 60 ] && [ $n -ge 0 ]then tag=1elif [ $n -ge 60 ] && [ $n -lt 80 ]then tag=2elif [ $n -ge 80 ] && [ $n -lt 90 ]then tag=3elif [ $n -ge 90 ] && [ $n -le 100 ]then tag=4else tag=0ficase $tag in 1) echo "not ok" ;; 2) echo "ok" ;; 3) echo "good" ;; 4) echo "very good" ;; *) echo "The number range is 0-100." ;; esac
Logical judgments in 69.shell scripts, file directory attribute judgments, if special usages, case judgments