Operators in Bash scripts

Source: Internet
Author: User
Tags bit set

First, the file test operator
If the following conditions are true, it will be returned.
-E
File exists
-A
File exists
This option has the same effect as-E. But it has been "deprecated" and is not encouraged to use it.
-F
Indicates that the file is a generic file (not a directory or device file)
-S
File size is not zero
-D
Indicates that this is a directory
-B
Indicates that this is a block device (floppy disk, optical drive, and so on.)
-C
Indicates that this is a character device (keyboard, modem, sound card, and so on.)
-P
This file is a conduit
-H
This is a symbolic link
-L
This is a symbolic link
-S
Indicates that this is a socket
-T
The file (descriptor) is linked to an end device
This test option is typically used to detect stdin in a script ([
Terminal.
-T 0])
or stdout ([
T 1]) is from a
-R
Whether the file has readable permissions (refers to whether the user who is running this test command has Read permission)-W
Whether the file has writable permissions (refers to whether the user who is running this Test command has write permission)
-X
Whether the file has executable permissions (refers to whether the user who is running this Test command has executable permissions)
-G
Set-group-id (SGID) tag is set to file or directory
If the directory has a sgid tag, the files created under this directory will belong to the user group that owns the directory, and
You do not have to be the user group that created the file. This feature is useful for sharing directories in a workgroup.
-U
Set-user-id (SUID) tag is set to file
If a root user has a binary executable that has the Set-user-id tag bit set, the average user will also
Run this file with root privileges. [1] This is for applications that require access to the system hardware (such as PPPD and Cdrecord) non-
Often used. If the SUID flag is not available, these binaries cannot be called by non-root users.
-rwsr-xr-t
1 root
178236 Oct
2
2000
/usr/sbin/pppd
For a file with the SUID flag set, it will be represented as s in its permission column.
-K
Set Paste Bit
For the general understanding of paste bit, the SAVE-TEXT-MODE flag is a special type of file permission. If the file sets the
Flag, the file will be saved to the cache, which can improve access speed. [2] Paste bit if set in the eye
It will restrict write permissions. For files or directories that have the paste bits set, they will be displayed in their permission tag columns
Display T.
Drwxrwxrwt
7 root
1024x768 21:26 tmp/
If the user does not own this directory with the paste bit set, but he has write permission in this directory, the user only
You can delete the files you own in this directory. This will effectively prevent users from accidentally overwriting or deleting in a common directory.
Except for someone else's file. such as the/tmp directory. (Of course, the owner or root user of the directory can delete or rename the
The file.)
-O
Determine if you are the owner of the file
-G
Is the Group-id of the file the same as yours?
-N
Whether the file has been modified since the last time it was read from the file
F1-nt F2
File f1 than file F2 new
F1-ot F2
File F1 F2 older than file
F1-ef F2 file F1 and file F2 are hard links to the same file
!

"Non"-reverses the result of all tests above (returns True if no condition is given).


Second, other comparison operators
A binary comparison operator is used to compare two variables or numbers. Note the difference between integer comparisons and string comparisons.
Integer comparison
-eq
Equals
If ["$a"-eq "$b"]
-ne
Not equal to
If ["$a"-ne "$b"]
-gt
Greater than
If ["$a"-gt "$b"]
-ge
Greater than or equal
If ["$a"-ge "$b"]
-lt
Less than
If ["$a"-lt "$b"]
-le
Less than or equal
If ["$a"-le "$b"]
<
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)
Next page (("$a" >= "$b"))
string comparison
=
Equals
If ["$a" = "$b"]
==
Equals
If ["$a" = = "$b"]
is equivalent to =.
The = = comparison operator behaves differently in double-bracket pairs and single-bracket pairs.


[[$a = = z*]]# If $ A starts with ' Z ' (pattern match) Then the result will be true [[$a = = ' z* '] # if $ A is equal to z* (which literally means exactly the same) then the result is true.  [$a = = z*]# file extension matching (Files globbing) and Word segmentation are valid. ["$a" = = "z*"] # if $ A is equal to z* (which is literally exactly the same), then the result is true.



!=
Unequal number
If ["$a"! = "$b"]
This operator will be in [[...]] Pattern matching is used in the structure.
<
Less than, sorted by ASCII character
if [["$a" < "$b"]
If ["$a" \< "$b"]
Note the "<" is used in the [
] Need to be escaped in the structure.
>
Greater than, sorted by ASCII character
if [["$a" > "$b"]
If ["$a" \> "$b"]
Note the ">" is used in the [
] Need to be escaped in the structure.
In reference to example 26-11, this example shows how to use this comparison operator.
-Z
The string is "null", meaning that the string length is zero
-N
The string is not "null".
When-n is used in the conditional test in brackets, the string must be quoted in double quotation marks. If you use a string that is not referenced! -Z, even in the conditional test bracket (parameter
See example 7-6) when using only unreferenced strings, it is generally possible to work, however,
It is a habit of insecurity. It is customary to use a referenced test string.


Compound comparison
-A
Logic and
Exp1-a EXP2
If both the expression Exp1 and Exp2 are true, then the result is true.
-O
Logical OR
Exp1-o EXP2
If at least one of the expressions Exp1 and Exp2 is true, then the result is true.
This is the comparison operator in Bash && and | | Very similar, but this two operator is used in the double-bracket structure.
[[Condition1 && Condition2]]
The-O and-a operators are generally used with the test command or the single-bracket structure.
If ["$exp 1"-A "$exp 2"]


Precautions
In a hybrid test, even using a referenced string variable may not be enough.
If $string is empty, [-N ' $string "-o" $a "=" $b "] may be generated in some versions of bash
Error. The safe approach is to append an extra character to the possible null variable, ["x$string"! = X-o "x$a" =
"X$b" ("X" characters can be offset from each other).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Operators in Bash scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.