Testing and comparison of shell scripts
Test command: Checks the file type and compares the value (depending on the execution result of the expression (true or false), returns an exit status. )
NAME
Test-check file types and compare values
DESCRIPTION
Exit with the status determined by EXPRESSION.
***************************************************************************************
File Test operators:
-d (directory) file exists and (&&) is directory, returns true
-F (file) file exists and (&&) is normal file, returns True
-E (exit) file exists, returns true
-R (read) file exists readable, returns true
-W (write) file exists writable, return false
-X (executable) file exists executable, returns true
-L (link) file exists and is a linked file, returns True
File1-nt file2 (Nt==>newer then), compared to the modified time of the file, the file file1 is newer than File2, and returns True
File1-ot file2 (Ot==>older then), compared to file modification time, file file1 older than File2, return true
To view the properties of a file: You can use LS and stat: for example:
$ stat. bash_history
File: '. Bash_history '
size:18690 blocks:40 IO block:4096 Regular file
device:802h/2050d inode:1049700 links:1
Access: (0600/-rw-------) Uid: (500/swallow) Gid: (500/swallow)
access:2017-01-23 14:44:54.134193735 +0800
modify:2017-01-23 05:43:16.301220988 +0800
change:2017-01-23 05:43:16.301220988 +0800
$ ls-l. bash_history
-RW-------. 1 Swallow Swallow 18690 Jan 05:43. bash_history
Example test:
$ file1=del.sh; file2=sum.sh
$ echo $file 1 $file 2
Del.sh sum.sh
$ ls-l $file 1 $file 2
-rw-rw-r--. 1 Swallow Swallow Bayi Jan 20:14 del.sh
-rw-rw-r--. 1 Swallow Swallow 16:55 sum.sh
$ test-f $file 1 && echo 0 | | echo 1 (test file1 is a normal file.) If return 0 is not return 1)
0
The test syntax can be abbreviated as [] but [] when writing, note the space between [] and the content.
$ [-D $file 2] && echo 0 | | Echo 1
1
$ [$file 1-nt $file 2] && echo 0 | | Echo 1
0
Extension: Two representations of Linux file permission system.
R: Represents a readable w: Represents a writable x: Represents the Rwx representation method that performs this is a permission:
The corresponding number representation method: R:4 w:2 x:1 is 4+2+1=7 (-:0 no permission is 0)
$ [x $file 1] && echo 0 | | Echo 1
1
We look at a system script to experience the use of test;
$ cat/etc/init.d/nfs
# Check for and source configuration file otherwise set defaults
[-f/etc/sysconfig/nfs] &&. /etc/sysconfig/nfs
*****************************************************************************************
String test operator, Comparison of 2 strings, test string length of 0 or null (empty string)
-N "string" (nonzero) string length is not 0 and returns true. That is, the string wants content
-Z "string" (zero) string length is 0 and returns true. The string is empty or does not exist.
"String 1" = "String 2" here = = = is used instead of
"String 1"! = "String 2" here = = = is used instead of
Focus: 1. Here is a good way to compare strings with "" parcels. such as "File1"
2. If the judgment is equal, = before and after spaces. Inside the computer = often used to assign values (var=123)
Test-n-z with a non-existing variable var
[Email protected] ~]$ echo $var
[[Email protected] ~]$ [-N "$var"] && echo 0 | | Echo 1
1
[[Email protected] ~]$ [-Z "$var"] && echo 0 | | Echo 1
0
***************************************************************************************
Logical operators:
-A (and, with, both ends are true, the result is true) can also be written in &&
-O (or, or, one at both ends is true and the result is true) can also be written as | |
! (not, non, opposite ends.) The result is true)
The logical operators are based on the individual judgments above. The combination judgment needs to be combined with the results of a single judgment.
Note:-a-o is used for [] Inside && | | More for [] outside
*************************************************************************************
Two-dollar comparer for integers
-eq (equal) (= = or =)
-ne (Not equal) (!== or! =)
-GT (greater than) (>)
-ge (Greater Equal) (>=)
-lt (less than) (<)
-le (less equal) (<=)
$ var1=23;var2=34
$ echo $var 1 $var 2
23 34
$ [$var 1-gt $var 2] && echo 0 | | Echo 1
1
This article is from the "Shong Linux Tour" blog, make sure to keep this source http://12042068.blog.51cto.com/12032068/1894143
Shell Experience Three