| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

03-12-04, 00:56
|
|
Registered User
|
|
Join Date: Jan 2003
Posts: 74
|
|
About the test command and its two alternative format
|
|
Hi,
I am a newbie for shell and I know the general format of the test command is
test expression
I read some books, some books show that the "test expression" can be expressed in the alternative format as
[ expression ], but others show that the "test expression" can be expressed in the alternative format as [[ expression ]].
Who can tell me what different between [ expression ] and [[ expression ]]?
Thanks in advance!!!!
|
|

03-12-04, 02:17
|
|
Registered User
|
|
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
|
|
[ ] is the same thing as the 'test' command.
[[ ]] is specific to ksh and bash.
It's a compatible evolution of test command, which offers more options.
__________________
Jean-Pierre.
|
|

03-12-04, 03:54
|
|
Registered User
|
|
Join Date: Jan 2003
Posts: 74
|
|
|
|
Thanks for your help! You said "[[ ]] offers more options". How can I find those options for [[ ]]?
|
|

03-12-04, 04:14
|
|
Registered User
|
|
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
|
|
man ksh
Quote:
Conditional Expressions.
A conditional expression is used with the [[ compound command to test attributes of files and to compare strings. Field splitting and file name generation are not performed on the words between [[ and ]]. Each expression can be constructed from one or more of the following unary or binary expressions:
string
True, if string is not null.
-a file
Same as -e below. This is obsolete.
-b file
True, if file exists and is a block special file.
-c file
True, if file exists and is a character special file.
-d file
True, if file exists and is a directory.
-e file
True, if file exists.
-f file
True, if file exists and is an ordinary file.
-g file
True, if file exists and it has its setgid bit set.
-k file
True, if file exists and it has its sticky bit set.
-n string
True, if length of string is non-zero.
-o option
True, if option named option is on.
-p file
True, if file exists and is a fifo special file or a pipe.
-r file
True, if file exists and is readable by current process.
-s file
True, if file exists and has size greater than zero.
-t fildes
True, if file descriptor number fildes is open and associated with a terminal device.
-u file
True, if file exists and it has its setuid bit set.
-w file
True, if file exists and is writable by current process.
-x file
True, if file exists and is executable by current process. If file exists and is a directory, then true if the current process has permission to search in the directory.
-z string
True, if length of string is zero.
-L file
True, if file exists and is a symbolic link.
-O file
True, if file exists and is owned by the effective user id of this process.
-G file
True, if file exists and its group matches the effective group id of this process.
-S file
True, if file exists and is a socket.
file1 -nt file2
True, if file1 exists and file2 does not, or file1 is newer than file2.
file1 -ot file2
True, if file2 exists and file1 does not, or file1 is older than file2.
file1 -ef file2
True, if file1 and file2 exist and refer to the same file.
string == pattern
True, if string matches pattern. Any part of pattern can be quoted to cause it to be matched as a string.
string = pattern
Same as == above, but is obsolete.
string != pattern
True, if string does not match pattern.
string1 < string2
True, if string1 comes before string2 based on ASCII value of their characters.
string1 > string2
True, if string1 comes after string2 based on ASCII value of their characters.
The following obsolete arithmetic comparisons are also permitted:
exp1 -eq exp2
True, if exp1 is equal to exp2.
exp1 -ne exp2
True, if exp1 is not equal to exp2.
exp1 -lt exp2
True, if exp1 is less than exp2.
exp1 -gt exp2
True, if exp1 is greater than exp2.
exp1 -le exp2
True, if exp1 is less than or equal to exp2.
exp1 -ge exp2
True, if exp1 is greater than or equal to exp2.
In each of the above expressions, if file is of the form /dev/fd/n, where n is an integer, then the test is applied to the open file whose descriptor number is n.
A compound expression can be constructed from these primitives by using any of the following, listed in decreasing order of precedence.
(expression)
True, if expression is true. Used to group expressions.
! expression
True if expression is false.
expression1 && expression2
True, if expression1 and expression2 are both true.
expression1 || expression2
True, if either expression1 or expression2 is true.
|
__________________
Jean-Pierre.
|
|

03-12-04, 04:56
|
|
Padawan
|
|
Join Date: Jun 2002
Location: UK
Posts: 525
|
|
One important difference to notice between the '[ ]' and '[[ ]]' constructs is that the latter will not expand file expressions (i.e. glob).
e.g.
Code:
if [[ * != "*" ]]
then
echo 'This will never be true! '
fi
if [ * != "*" ]
then
echo 'This will be true if the directory is not empty and the * is expanded.'
fi
Damian
|
|

03-14-04, 19:24
|
|
Registered User
|
|
Join Date: Jan 2003
Posts: 74
|
|
Thanks for all your help! I will try it.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|