Logical judgments in shell scripts
Format 1:if condition; Then statement; Fi
If a>3, output OK
#!/bin/basha=5if [ $a -gt 3 ]then echo okfi
Format 2:if condition; Then statement; else statement; Fi
If the a>8 output is OK, the output nook
[[email protected]]# vim if1.sh#!/bin/basha=5if [ $a -gt 8 ]then echo okelse echo nookfi
Format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi
a=5if [ $a -gt 1 ]then echo ">1"elif [ $a -lt 6 ]then echo "<6 && >1"else echo nookfi
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 && | | Combine multiple conditions
If [$a-gt 5] && [$a-lt 10]; Then
If [$b-gt 5] | | [$b-lt 3]; Then
-GT greater than (>)
-lt less Than (<)
-eq equals (= =)
-ne not equal to (! =)
-ge greater than or equal to (>=)
-le less than or equal (<=)
File Directory property judgment
In the shell, it is often necessary to deal with files or directories, so it is important to judge their attributes.
[-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
Determine if the Lx.txt file exists in the/tmp directory, if any, the output is not created
#!/bin/bashf="/tmp/lx.txt"if [ -f $f ]then echo $f existelse touch $ffi
If special usage
If [-Z ' $a '] This indicates what happens when the value of variable A is empty
#!/bin/bashn=‘wc -l /tmp/test.txt‘if [ -z "$n" ]then echo error exitelif [ $n -gt 100 ]then echo "ok"fi
f [-N "$a"] means that when 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 []
Case Judgment
The case statements in the shell and the switch statements in other programming languages mean the same thing, except that the syntax in the shell looks a little bit more bizarre, with the case keyword declaring the condition.
Case this conditional judgment statement is the decision of the tag, that is, when the variable conforms to a value (value), the code block inside the value is executed, for example, when the value of variable A is 1 o'clock, it will match the code block with the value 1 in the case code block, if it does not match the corresponding value will be executed *
), the code in the shell needs to be used in the case statement;; To jump out of a statement, which is the same as the break meaning of other languages, because of the readability problem, so case is more complicated than if.
格式 case 变量名 in value1) command ;; value2) command ;; *) commond ;; esac
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" ]thenecho "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 "ook" ;; 4)echo "oook" ;; *) echo "The number range is 0-100." ;;esac
Logical judgments in shell scripts, file directory attribute judgments, if special usages, case judgments