File Status Test
-B filename |
Returns True when filename exists and is a block file (returns 0) |
-C filename |
Returns True when filename exists and is a character file |
-D Pathname |
Returns True when pathname exists and is a directory |
-E Pathname |
Returns True when a file or directory specified by pathname is present |
-F filename |
Returns True when filename exists and is a regular file |
-G pathname |
Returns True when the file or directory specified by pathname exists and the Sgid bit is set |
-H filename |
Returns True when filename exists and is a symbolic link file (or-l filename) |
-K Pathname |
Returns True when a file or directory specified by pathname exists and the "sticky" bit is set |
-P filename |
Returns True when filename exists and is named pipe |
-R Pathname |
Returns True when a file or directory specified by pathname exists and is readable |
-S filename |
Returns True when filename exists and the file size is greater than 0 o'clock |
-S filename |
Returns True when filename exists and is a socket |
-T FD |
When FD is a file descriptor associated with a terminal device, it returns true |
-U pathname |
Returns True when the file or directory specified by pathname exists and the SUID bit is set |
-W Pathname |
Returns True when a file or directory specified by pathname exists and is writable |
-X Pathname |
Returns True when a file or directory specified by pathname exists and is executable |
-O Pathname |
Returns True when a user owned by a valid user ID of the current process exists and is pathname (Letter O Uppercase) |
-G pathname |
True when a user group that is present by pathname and that is part of the current process's valid user ID is returned |
File1-nt File2 |
File1 than File2 new when return True |
File1-ot File2 |
File1 than file2 back to true |
F1-EF F2 |
Files F1 and F2 is hard links to the same file |
Example: if [-b/dev/hda]; then echo "yes"; else echo "no"; fi//will print Yes
Test-c/dev/hda; echo $? Will print 1 to indicate that the return value of the test command is 1,/DEV/HDA not a character device
[-W/ETC/PASSWD]; echo $? See if the passwd file is writable for the current user
Test-time logical operator
-A |
Logical AND, both sides of the operator are true, the result is true, otherwise false. |
-O |
Logical OR, the operator side is true, the result is true, otherwise false. |
! |
Logical No, the condition is false and the result is true. |
Example: [-w Result.txt-a-W Score.txt]; echo $? Test whether two files are writable
Common string tests
-Z String |
string is empty (length 0) returns True |
-N String |
Returns True when string strings are non-empty strings |
STR1 = str2 |
Returns True when String str1 and string str2 are equal |
STR1 = = str2 |
same = |
Str1! = str2 |
Returns True when String str1 and string str2 are not equal |
STR1 < STR2 |
Sort by dictionary order, string str1 before string str2 |
str1 > STR2 |
Sort by dictionary order, string str1 after string str2 |
Example: Name= "ZQF"; [$name = "ZQF"];echo $? Printing 0 means the value of the variable name and the string "ZQF" are equal
Common numerical tests
Int1-eq Int2 |
If Int1 equals Int2, returns the true |
Int1-ne Int2 |
Returns true if Int1 is not equal to Int2 |
Int1-lt Int2 |
If Int1 is less than Int2, returns true |
Int1-le Int2 |
Returns True if Int1 is less than or equal to Int2 |
INT1-GT Int2 |
If Int1 is greater than Int2, returns true |
Int1-ge Int2 |
Returns true if Int1 is greater than or equal to Int2 |
Tests in (()):
< |
Less than (used in double brackets) |
(("$a" < "$b")) |
<= |
Less than equals (used in double brackets) |
(("$a" <= "$b")) |
> |
Greater than (used in double brackets) |
(("$a" > "$b")) |
>= |
Greater than or equal (used in double brackets) |
(("$a" >= "$b")) |
Example: x=1; [$x-eq 1]; echo $? Printing 0 means that the value of the variable x equals the number 1 x=a; [$x-eq "1"]//Shell print error message [: A:integer expression expected
Test, [], [[]]
Because the shell differs from our usual programming language, it's more about interacting with it and always calling someone else. So some of the concepts that are part of the programming language itself can be difficult to understand in the shell. "Basic skills" is not good, more easily "sleepy", I am a:-).
Take bash for example (similar to other compatible shells):
- Test and [is the internal command of bash, the coreutils package of the Gnu/linux system usually also takes/usr/bin/test and/usr/bin/[commands. If we don't specify the absolute path, we usually use Bash's own commands.
- [[Is the key word of the BASH program language!]
$ ls-l/usr/bin/[/usr/bin/test-rwxr-xr-x 1 root root 37400 September 15:25/usr/bin/[-rwxr-xr-x 1 root root 33920 September 15:25/usr/bin/test$ type [[[[Test[is a shell builtin[[are a shell keywordtest is a shell builtin
In most cases, this three function is common. But commands and keywords are always different. How big is the difference between a command and a keyword?
If it is a command, it is interpreted by the shell as a combination of parameters, such as ">" < "is interpreted by the shell as the redirect symbol." Keywords are not.
Use && in [[] | |
[Use-A and-O are used to represent logic and logic or.
[You can use wildcard characters in
arch=i486[[$arch = i*86]] && echo "Arch is x86!"
[[Match string or wildcard character, no quotation marks required
[[$arch _com = i386 | | $ARCH = i*86]] && cat >> $TFS _repo <<eof[tfs-i386]name=gtes11.3 Prelim1bas Eurl=${baseurl}i386/enabled=1eof
Shell condition Test