20.5 Logical judgment in shell scripts
Format 1: if condition; then statement; fi
Format 2: if condition; then statement; else statement; fi
格式3:if …; then … ;elif …; then …; else …; fi
Logical judgment expression:
if [ $a -gt $b ];
if [ $a -lt 5 ];
if [ $b -eq 10 ]
-gt (>); -lt(<); -ge(>=) ; -le(<=);-eq(==); -ne(!=) Notice the spaces everywhere
Multiple conditions can be combined using && ||
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then
20.6 Judgment of file directory attributes
[ -f file ] Determine whether it is a normal file and exists
[ -d file ] Determine whether it is a directory and exists
[ -e file ] Determine if a file or directory exists
[ -r file ] Determine if the file is readable
[ -w file ] Determine if the file is writable
[ -x file ] Determine if the file is executable
20.7 Special uses of if
if [ -z "$a" ] This means what happens when the value of variable a is empty
if [ -n "$a" ] means when the value of variable a is not empty
if grep -q '123' 1.txt; then means what happens if 1.txt contains a line of '123'
if [ ! -e file ]; then represents what happens when the file does not exist
if (($a\<1)); then … is equivalent to if [ $a -lt 1 ]; then …
[ ] cannot use <, >,==,!=,>=,<= such symbols
20.8/20.9 case judgment
Format case variable name in
value1
command
;;
value2)
command
;;
*)
commond
;;
esac
In the case program, you can use | in the condition to mean or, such as
2|3)
command
;;