Shell if detailed explanation
Judgment parameters in if statements used in shell programming
-b returns true when file exists and is a block file
-c returns true when file exists and is a character file
-d returns true when pathname exists and is a directory
-e returns true if the file or directory specified by pathname exists
-f returns true when file exists and is a regular file
-g returns true when the file or directory specified by pathname exists and the SGID bit is set
-h returns true when file exists and is a symbolic link file, this option is not valid on some older systems
-k returns true when the file or directory specified by pathname exists and the "sticky" bit is set
-p returns true when file exists and is a command pipeline
-r returns true when the file or directory specified by pathname exists and is readable
-s returns true when the file size is greater than 0 in file
-u returns true if the file or directory specified by pathname exists and the SUID bit is set
-w returns true when the file or directory specified by pathname exists and is executable. A directory must be executable to be accessed for its contents.
-o returns true when the file or directory specified by pathname exists and is owned by the user specified by ID, a valid user of the current process.
Compare the characters written in UNIX Shell:
-eq equals
-ne is not equal to
-gt is greater than
-lt less than
-le less than or equal to
-ge greater than or equal to
-z empty string
= two characters are equal
! = two characters are different
-n non-empty string
A more detailed explanation:
Operator description example
File comparison operator
-e filename true if filename exists [- e / var/log/syslog]
-d filename true if filename is a directory [- d / tmp/mydir]
-f filename True [- f / usr/bin/grep] if filename is a regular file
-L filename is true if filename is a symbolic link [- L / usr/bin/grep]
-r filename true if filename is readable [- r / var/log/syslog]
-w filename is true if filename is writable [- w / var/mytmp.txt]
-x filename true if filename is executable [- L / usr/bin/grep]
Filename1-nt filename2 is true if filename1 is newer than filename2 [/ tmp/install/etc/services-nt / etc/services]
Filename1-ot filename2 is true if filename1 is older than filename2 [/ boot/bzImage-ot arch/i386/boot/bzImage]
String comparison operator (note the use of quotation marks, which is a good way to prevent spaces from disturbing the code)
-z string true [- z $myvar] if string length is zero
-n string true [- n $myvar] if the string length is non-zero
String1 = string2 true if string1 is the same as string2 [$myvar = one two three]
String1! = string2 true if string1 is different from string2 [$myvar! = one two three]
Arithmetic comparison operator
Num1-eq num2 equals [3-eq $mynum]
Num1-ne num2 is not equal to [3-ne $mynum]
Num1-lt num2 is less than [3-lt $mynum]
Num1-le num2 is less than or equal to [3-le $mynum]
Num1-gt num2 is greater than [3-gt $mynum]
Num1-ge num2 is greater than or equal to [3-ge $mynum]
Example script:
#! / bin/bash
# This script prints a message about your weight if you give it your
# weight in kilos and hight in centimeters.
If [! $# = = 2]; then
Echo "Usage: $0 weight_in_kilos length_in_centimeters"
Exit
Fi
Weight= "$1"
Height= "$2"
Idealweight=$ [$height-110]
If [$weight-le $idealweight]; then
Echo "You should eat a bit more fat."
Else
Echo "You should eat a bit more fruit."
Fi
# weight.sh 70 150
You should eat a bit more fruit.
# weight.sh 70 150 33
Usage:. / weight.sh weight_in_kilos length_in_centimeters
The location parameter $1, $2dr., $N # represents the number of arguments on the command line, and $0 represents the name of the script.
The first parameter represents $1, the second parameter represents $2, and so on, the total number of parameters exists in $#, and the above example shows how to change the script to print a message if there are fewer or more parameters.
Execute and check the situation.
# bash-x tijian.sh 60 170
+ weight=60
+ height=170
+ idealweight=60
+'['60-le 60']'
+ echo 'You should eat a bit more fat.'
You should eat a bit more fat.
Where-x is used to check the execution of the script.