68. Logical judgment in shell script, file directory attribute judgment, if special usage, case judgment
I. Logical judgments in shell scripts
Format 1:if condition; Then statement; Fi
Format 2:if condition; Then statement; else statement; Fi
Format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi
if: If.
Then: and then.
-GT: Greater than.
-LT: Less than.
-eq: Equals.
-ne: Not equal to. Noeq.
-ge: greater than or equal.
-le: Less than equals.
Format 1: If a condition a greater than 3 is met, the output is OK. Most commonly used.
# if [$a-gt 3]; then echo OK; FI//This is a one-line command to represent the script below
#!/bin/basha=5if [$a-gt 3]then echo OKFI
# sh-x if1.sh//Execution Process: 5 and 3 comparison, greater than 3, output OK. + a=5+ ' [' 5-gt 3 '] ' + echo Okok
Format 2: If the condition is not satisfied no more than 3, the output Nook
#!/bin/basha=5if [$a-gt 3]then echo okelse echo NOOKFI
# sh-x if2.sh + a=2+ ' [' 2-gt 3 '] ' + echo Nooknook
Format 3: The first condition is not met, but the second condition is met.
#!/bin/basha=2if [$a-gt 4]then echo ">1" elif [$a-lt 3]then echo "<3 && >4" Else echo NOOKFI
# sh-x if3.sh + a=2+ ' [' 2-GT 4 '] ' + ' [' 2-lt 3 '] ' + echo ' <3 && >4 ' <3 && >4
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.
&&: And.
|| Or
If [$a-gt 5] && [$a-lt 10]; Then
If [$b-gt 5] | | [$b-lt 3]; Then
Second, the file directory attribute judgment
[-F file] Determines whether it is a normal file, whether it exists
[-D file] Determines whether it is a directory, whether it 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
#!/bin/bashf= "/tmp/aminglinux" if [-F $f]then echo $f existelse touch $ffi
# Sh-x file.sh//First judgment, without this file, will create this file
+ f=/tmp/aminglinux+ ' ['-f/tmp/aminglinux '] ' + touch/tmp/aminglinux
# sh-x file.sh//second judgment, have this file, directly output this file
+ f=/tmp/aminglinux+ ' ['-f/tmp/aminglinux '] ' + echo/tmp/aminglinux exist/tmp/aminglinux exist
#!/bin/bashf= "/tmp/aminglinux" [-f/tmp/aminglinux] && rm-f $f
Determine if the previous file exists, and if so, delete it. If [-F $f]thenrm-f $ffi
[-f/tmp/aminglinux] && rm-f $f//This sentence is equal to the above four sentences.
[-F $f] | | Touch $f//When the previous command execution is unsuccessful, the following command is executed. Equal to the following four sentences. -F Inverse. When this file does not exist, touch it.
if [!-F $f]
Then
Touch $f
Fi
Third, if special usage
If [-Z ' $a '] This indicates what happens when the value of variable A is empty
If [-n ' $a '] means that when the value of variable A is not empty, it is not empty and the condition is true.
! -Z =-N
! -n =-Z Their relationship is relative
when you use-Z and-N to judge, variables enclosed in double quotes, the file is not used.
When using this usage, do not use elif, there is no meaning, you should use else.
#!/bin/bashn= '/tmp/lala ' if [-Z ' $n "]thenecho noexitelseecho" 12345 ">>"/tmp/lala "fi
If you use elif like below to write, it is not right, because the front is a file, the back with elif can only be compared with the number of judgments, because the file and the number can not be compared, so will error, directly with else, and then touch the file or add something:
# if [-n/tmp/lala];then echo Okk; Fi
#!/bin/bashn= '/tmp/lala ' if [-Z ' $n ']thenecho noexitelif [$n-gt]thenecho YESFI
If Grep-q ' 123 ' 1.txt; Then what happens if the 1.txt contains a ' 123 ' row
-W: more accurate matching.
-Q: Filter, but do not display the filtered content.
# if Grep-wq ' user1 '/etc/passwd; Then echo "User1 exist"; Fi
if [!-e file]; Then what happens when the file doesn't exist?
!: Take the inverse, exclamation point.
if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ...
Symbols such as <,>,==,!=,>=,<= cannot be used in []
Iv. Case Judgment
Format case variable name ($a) in
value1)
Command
;;
value2)
Command
;;
*)
Commond
;;
Esac
In a case program, you can use a |, meaning, or means in a condition, such as
VALUE2|VALUE3)
Command
;;
Each judgment ends with a double semicolon, which represents the end of judgment and enters the next judgment.
When the status is executed, the following statement is executed, and when executing restart or reload or force-reload, it executes the following statement, executing the statement under "*" when other commands are executed.
# read-p "Please input a number:" N read-p of the function : For example, assign a value to N and enter what is what.
Please input a number:12345
# echo $n
12345
#!/bin/bashread -p "please input a number: " nif [ -z "$n" ]thenecho "Please input a number." exit 1 //exit, return a value fin1= ' echo $n |sed ' s/[0-9]//g ' determine if the input variable is not a pure digital if [ -n "$n 1" ]thenecho "please input a number. " Enter a pure digital exit 1 Back out fiif [ $n -lt 60 ] && [ $n -ge 0 ] $n less than 60 and greater than or equal to 0thentag=1 first mark with Tag=1 elif [ , $n -ge 60 ] && [ $n -lt 80 ] $n greater than 60 less than 80 thentag=2elif [ with tag=2 Mark $n -ge 80 ] && [ $n -lt 90 ] $n is greater than or equal to 80 and less than 90 is marked thentag=3elif [ with tag=3 $n -ge 90 ] && [ $n -le 100 ] $n greater than or equal to 90 and less than or equal to 100 with tag= 4 = thentag=4else is not the case of tag=0tag=0ficase $tag in1) echo "Not ok";; 2) echo "OK"; 3) echo "ook";; 4) echo "Oook";; *) echo "the number range is 0-100.";; Esac
Test 1:
# sh -x case.sh+ read -p ' please input a number: ' nplease input a number: 101+ ' [' -z 101 '] //first to judge if this variable is present ++ echo 101++ sed ' s/[0-9]//g ' After //exists, make a judgment, empty the numbers, and then n1 the empty + n1=+ ' [' -n ' ' //judge whether it is not empty, not empty is normal, and then go down, a comparison of judgment. + ' [' 101 -lt 60 '] ' + ' [' 101 -ge 60 '] ' + ' [' 101 -lt 80 ' + ' [' 101 -ge 80 '] ' + ' [' 101 -lt 90 ' + ' [' 101 -ge 90 '] ' + ' [' 101 -le 100 '] ' + tag =0 // All conditions are not met, last tag=0+ case $tag in //tag=0 is not in 1234, it returns the output of the * number. + echo ' the number range is 0-100. ' The number range is 0-100.
Test 2:
# sh-x case.sh+ read-p ' Please input a number: ' nplease input a number:65+ ' ['-z + '] ' + echo 65++ sed ' s/[0-9]//g ' + n1=+ ' ['-n '] ' + ' [' 65-lt '] ' + ' [' 65-ge '] ' + ' [' 65-lt '] ' + tag=2+ case $tag in//greater than 60 And less than 80, directly determine tag=2+ echo Okok
68. Logical judgment in shell script, file directory attribute judgment, if special usage, case judgment