With test operator, you can check condition in shell script which is already discussed in http://arjudba.blogspot.com/2009/04/checking-condition-in-shell-scipt-by.html. In this post I am trying to stand a list of those comparisons that can be used by test operator in shell script.
A) File Test Comparison
1)-e : If -e is used inside test condition then it returns true if file exists.
2)-a : Same as -e. If file exists return true. But it has been "deprecated," and so its use is discouraged.
3)-f : Returns true if file is a regular file (not a directory or device file).
4)-s : Returns true if file is not zero size.
5)-d : Returns true if file is a directory.
6)-b : Returns true if file is a block device.
7)-c : Returns true if file is a character device.
8)-p : Returns true if file is a pipe.
9)-h : Returns true if file is a symbolic link.
10)-L : Returns true if file is a symbolic link.
11)-S : Returns true if file is a socket.
12)-t : Returns true if file is associated with a terminal device.
13)-r : Returns true if file has read permission. This is for the user who is running test operation.
14)-w : Returns true if file has write permission (for the user who runs the test).
15)-x : Returns true if file has execute permission (for the user who runs the test).
16)-g : Returns true if set-group-id (sgid) flag set on file or directory.
17)-u : Returns true if set-user-id (suid) flag set on file.
18)-k : Returns true if sticky bit set.
Sticky bit is a special type of file permission.
If a file has this flag set, that file will be kept in cache memory, for quicker access.
If set on a directory, it restricts write permission.
Setting the sticky bit adds a t to the permissions on the file or directory listing.
Just like below.
drwxrwxrwt 1 root 1024 Aug 1 11:21 arju/
If a user does not own a directory that has the sticky bit set, but has write permission in that directory, he can only delete those files that he owns in it. This keeps users from inadvertently overwriting or deleting each other's files in a publicly accessible directory, such as /tmp. (The owner of the directory or root can, of course, delete or rename files there.)
19)-O : Returns true if you are owner of the file.
20)-G : Returns true if group-id of file same as yours.
21)-N : Returns true if file modified since it was last read.
22)f1 -nt f2 : Returns true if file f1 is newer than f2
23)f1 -ot f2 : Returns true if file f1 is older than f2
24)f1 -ef f2 : Returns true if files f1 and f2 are hard links to the same file.
25)! : It is "not". Just reverses the sense of the tests. Note that it returns true if condition is not present.
B) Integer Comparison
1)-eq : -eq is same like equal to.
if [ "$a" -eq "$b" ] # if value of a is equal to value of b.
2)-ne : indicates is not equal to.
if [ "$a" -ne "$b" ] # if value of a is not equal to b.
3)-gt : indicates is greater than
if [ "$a" -gt "$b" ] # if value of a is greater than b.
4)-ge : indicates is greater than or equal to
if [ "$a" -ge "$b" ] # if value of a is greater than or equal to value of b.
5)-lt : indicates is less than
if [ "$a" -lt "$b" ] # if value of a is less than the value of b.
6)-le : indicates is less than or equal to
if [ "$a" -le "$b" ] # if value of a is less than or equal to value of b.
7)< : is less than (use it within double parentheses) (("$a" < "$b")) # if value of a is less than value of b.
8)<= : is less than or equal to (use it within double parentheses) (("$a" <= "$b")) # if value of a is less than or equal to b.
9)> : is greater than (use it within double parentheses)
(("$a" > "$b")) # if value of a is greater than value of b.
10)>= : is greater than or equal to (use it within double parentheses)
(("$a" >= "$b")) # if value of a is greater than or equal to value of b.
C)String Comparison
1)= : indicates is equal to
if [ "$a" = "$b" ]
2)== : indicates is equal to
if [ "$a" == "$b" ]
This is a synonym for =.
The == comparison operator behaves differently within a double-brackets test than within single brackets.
[[ $a == z* ]] # True if $a starts with an "z" (regex pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).
[ $a == z* ] # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).
3)!= : indicates is not equal to
if [ "$a" != "$b" ]
use pattern matching within [[ .... ]] construct.
4)< : is less than, in ASCII alphabetical order if [[ "$a" < "$b" ]] if [ "$a" \< "$b" ] Note that the "<" needs to be escaped within a [ ] construct.
5)> : is greater than, in ASCII alphabetical order
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
Note that the ">" needs to be escaped within a [ ] construct.
6)-z : string is null, that is, has zero length
String='' # Zero-length ("null") string variable.
if [ -z "$String" ]
then
echo "\$String is null."
else
echo "\$String is NOT null."
fi # $String is null.
7)-n : string is not null.
The -n test requires that the string be quoted within the test brackets. Using an unquoted string with ! -z, or even just the unquoted string alone within test brackets normally works, however, this is an unsafe practice. Always quote a tested string.
D)Compound Comparison
1)-a : logical and
exp1 -a exp2 returns true if both exp1 and exp2 are true.
2)-o : logical or
exp1 -o exp2 returns true if either exp1 or exp2 is true.
These are similar to the Bash comparison operators && and ||, used within double brackets.
Related Documents
Saturday, October 24, 2009
Subscribe to:
Posts (Atom)
Tag Cloud
SQL
Troubleshooting
Performance
RMAN
UNIX
Data Pump
Shell Script
PHP
Recovery
Oracle
Security
Backup
Parameters
Data Dictionary
Linux
Sql*Plus
Others
PL/SQL
Tablespaces
Data Type
Internet
Flashback
Globalization Support
Concepts
EM
Windows Tips
initializaion parameter
Explain plan
Installation
Server Administration
Export
Import
Packages
CSS
Spfile
Controlfiles
Functions
Joins
RAC
Utilities
Windows
Administration
DBConsole
Data Block
Limitation
MySQL
Oracle Recovery
Pfile
Indexes
Listener
Redo Log
Startup Problem
Net Services
Oracle Concepts
Archival
Audit
Database Administration
Internals
SEO
Solaris
About Oracle
Connection
Recovery Problems
Scripts
Temp
Alerts
Bug
Firmware
OUI
RAC Installation
SGA
Block Corruption
Clusterware
Network
Package
Partitioning
Profile
Undo
Version
10.2g
Browser
HTML
Logminer
OS
Operators
Pseudocolumns
RMAN Problem
Restore Problem
SwingBench
Tools
Bangladesh
Business
Clone
Data Guard
Forum
IE
Magento
Mail
Materialized View
Medical
Memory
Quota
Streams
TNS Error
Technology
Views
11g
Comments
Crime
Economics
External Table
Firefox
History
Java
JavaScript
Job
Joke
Money
Multimedia
Patchset
Perl
Puzzle
RMAN Backup
Reports
SQL Tuning
SQL*Loader
Smarty
Tuning
Vmware
isql*plus

