20.5 logical judgments in shell scripts
You should use a variable instead when you use a string more frequently in your script and the string length is long
When using conditional statements, variables are often used?? If [$a-gt 1]; Then ...; Fi
When referencing the result of a command, replace it with a variable?? n=
wc -l 1.txt
When writing scripts for user interaction, variables are also necessary?? Read-p "Input a number:" N; echo $n?? If you don't write this n, you can use $reply directly.
Built-in variables, $ $, $ ...? ? $ A represents the script itself, the first parameter, the second ...? ?? ? $ #表示参数个数
Mathematical Operation a=1;b=2; c=$ (($a + $b)) or $[$a + $b]
Format 1:if condition; Then statement; Fi
Format 2:if condition; Then statement; else statement; Fi
Format 3:if ...; Then ...; Elif ...; Then ...; else ...; 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 && | | Combine multiple conditions
If [$a-gt 5] && [$a-lt 10]; Then
If [$b-gt 5] | | [$b-lt 3]; Then
20.6 File Directory property judgment
[-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
20.7 If special usage
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 []
20.8/20.9 Case Judgment
The Vim test.sh script is as follows:
#!/bin/bash
Read-p "Please input a number:" N
If [-Z $n]
Then
echo "Please input a number:"
Exit 1
Fi
m=echo $n|sed ‘s/[0-9]//g‘
if [!-Z $m]
Then
echo "Please input a number:"
Exit 1
#elif [$n-lt 0] | | [$n-GT 100]
#then
echo "The number Ranger is 0-100."
#exit 1
Fi
If [$n-lt] && [$n-ge 0]
Then
Tag=1
elif [$n-ge] && [$n-LT 80]
Then
tag=2
elif [$n-ge] && [$n-LT 90]
Then
Tag=3
elif [$n-ge] && [$n-le 100]
Then
Tag=4
Else
Tag=0
Fi
Case $tag in
1)
echo "Not OK"
;;
2)
echo "OK"
;;
3)
echo "Good"
;;
4)
echo "Very good"
;;
*)
echo "The Numbet range 0-100."
;;
The test is as follows:
20.5 logical judgment in shell scripts 20.6 file Directory Properties Judge 20.7 if special usage 20.8/