Question: '[[ test ]]' vs. '[ test ]'

Question: '[[ test ]]' vs. '[ test ]'

Post by Jack Du » Wed, 20 Jan 1999 04:00:00



Hi,

I am a little confused if the following shell testing clauses are the
same, or, if not, what would be the benefit for using one over the other?

Using /bin/sh,

[[ -f /tmp/myfile ]] && echo "cool, file exists!"

or

[ -f /tmp/myfile ]   && echo "cool, file exists!"

I've always used the second form, but in HP-UX's /sbin/init.d/* init scripts,
most lines use the first form, so, is there any good vs bad thing to use one
of them instead of the other?

Thanks!  Please send a copy of your reply via email... many thanks!

Jack Duan

--

 UNIX System Administrator/Specialized Consultant
^------------------------------------------------------------------^

 
 
 

Question: '[[ test ]]' vs. '[ test ]'

Post by Cal Duniga » Wed, 20 Jan 1999 04:00:00



Quote:> Hi,

> I am a little confused if the following shell testing clauses are the
> same, or, if not, what would be the benefit for using one over the other?

> Using /bin/sh,
> [[ -f /tmp/myfile ]] && echo "cool, file exists!"
> or
> [ -f /tmp/myfile ]   && echo "cool, file exists!"

> I've always used the second form, but in HP-UX's /sbin/init.d/* init scripts,
> most lines use the first form, so, is there any good vs bad thing to use one
> of them instead of the other?

The [[ ... ]] construct will not work in a bourne shell script.  The
bourne shell has no internal testing capability, and one has to rely
on the Unix test command which can be invoked as either:
   test expression
   [ expression ]

The Korn shell does have the ability, and of course it can also invoke
Unix programs just like any other shell.  The [[ expression ]]
construct invokes the KSH builtin testing feature, which is obviously
much faster than executing an external program.

The default shell under HPUX is ksh, not sh, so in scripts where the
#! is ommitted from the first line, ksh is invoked to interpret it.
In short, the init files you looked at were Korn shell scripts.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

      Consulting                   wrong with a world in which Ken
      Modeling                     Thompson lives in obscurity and
      Training                     Bill Gates is a famous billionaire.
//////////////////////////////////////////////////////////////////////

 
 
 

Question: '[[ test ]]' vs. '[ test ]'

Post by John McKow » Wed, 20 Jan 1999 04:00:00


My "UNIX in a Nutshell" book says (qouting)

[[expression]]

Korn shell only. Same as "test expression" or "[ expression ]", except that
[[ ]] allows additional operators. Word splitting and filename expansion are
disabled. Additional operators
&& logical AND of test expressions
|| logical OR of test expressions

Quote:> First string is lexically "greater than" the second

< First string is lexically "less than" the second
 
 
 

Question: '[[ test ]]' vs. '[ test ]'

Post by Stephane Du Pasquie » Thu, 21 Jan 1999 04:00:00


Quote:> The default shell under HPUX is ksh, not sh, so in scripts where the
> #! is ommitted from the first line, ksh is invoked to interpret it.
> In short, the init files you looked at were Korn shell scripts.

The default for root (and the one that is used for those startup
scripts) *is* actually "sh", but this sh is the POSIX-shell
(which is nothing else than a POSIX-blessed Korn shell, under HP-UX).

S.

 
 
 

Question: '[[ test ]]' vs. '[ test ]'

Post by John DuBo » Fri, 22 Jan 1999 04:00:00




>My "UNIX in a Nutshell" book says (qouting)

>[[expression]]

>Korn shell only. Same as "test expression" or "[ expression ]", except that
>[[ ]] allows additional operators. Word splitting and filename expansion are
>disabled. Additional operators
>&& logical AND of test expressions
>|| logical OR of test expressions

Note that these are *instead* of -a and -o - you can't use -a or -o in [[]].

Quote:>> First string is lexically "greater than" the second
>< First string is lexically "less than" the second

Also, you can use parentheses for controlling the order of evaluation,
and (the most common reason for me to use [[]] instead of []), = does
matching using ksh patterns, e.g.  [[ $j = */* ]]

        John
--

 
 
 

1. 'test' or 'if' for an accessed file

Hello,

Bourne Shell:
I was wondering if anyone new how I can test if a file was being
accessed by a background process before I carry out and operation on it
from another background process.

I was thinking of locking the files by  'chmod' +l during one background
process but I would rather have a way of testing the file access with
the 'test' or 'if' command.
i.e.
if [ -something <filename> ]

Help Appreciated
Thanks

Guido.

2. Config.help misleading

3. TEST TEST TEST; don't read

4. Bogomips on ST486DX-2/80 ??

5. /bin/test vs. /bin/sh's internal test

6. Kernel Modules to support Quake behind a firewall

7. grep octal value (grep '\0145' /tmp/test) doesn't work !!

8. Help: need to raise memory avail to processes!

9. X windows question 'root' vs 'user'

10. test test test test test test test

11. 'su ' vs. 'su -'

12. kermit help: 'make bsd' and 'kermit vs watchdog'

13. 'priority' vs. 'nice'