Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One or two square brackets 2

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
All

I'm working in ksh on AIX 5.1. Can anyone explain the difference between the [ condition ] and [[ condition ]]. for example, an actual screen scrape
Code:
b01301$ ls -d one*
one            one.two.three  one.txt        one_list       onedir
b01301$ [ -f one* ] && echo yes || echo no
yes
b01301$ [[ -f one* ]] && echo yes || echo no
no

Thanks


On the internet no one knows you're a dog

Columb Healy
 
Hi

The double bracket ( [ ) is newer than single bracket. Also faster and enhanced. If you not care about portability is suggested to use double bracket.

Regarding your test, I never saw something like that, but I would not expect it to work anyway.

Feherke.
 
man test
man ksh

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The single bracket only ever acts on the first item in the shell expanded list eg as non root
spcws:/u/test> ll
total 0
-rw-r--r-- 1 root system 0 Oct 06 13:46 a1
-rw-r--r-- 1 root system 0 Oct 06 13:46 a2
-rw-r--r-- 1 root system 0 Oct 06 13:46 a3
-rw------- 1 root system 0 Oct 06 13:46 a4
spcws:/u/test> [ -r a* ]||echo N
spcws:/u/test>
spcws:/u/test> [ -r a4 a1 ]||echo N
N
spcws:/u/test> [ -r a1 a4 ]||echo N
spcws:/u/test>
On the other hand [[ seems to evaluate literally:
spcws:/u/test> [[ -r a4 a1 ]]||echo N
ksh: 0403-057 Syntax error: `a1' is not expected.
spcws:/u/test> [[ -r a* ]]||echo N
N
spcws:/u/test> touch a\*
spcws:/u/test> ll
total 0
-rw-r----- 1 user root_su 0 Oct 07 15:14 a*
-rw-r--r-- 1 root system 0 Oct 06 13:46 a1
-rw-r--r-- 1 root system 0 Oct 06 13:46 a2
-rw-r--r-- 1 root system 0 Oct 06 13:46 a3
-rw------- 1 root system 0 Oct 06 13:46 a4
spcws:/u/test> [[ -r a* ]]||echo N
spcws:/u/test>

I'm not sure what meaning a -r or -f test would have if it could process multi files

 
Thanks Andy
That's what I was looking for. Have a star.

On the internet no one knows you're a dog

Columb Healy
 
The [[ is processed by ksh, so there is no shell expantion, a [ calls a shell with test.



Tony ... aka chgwhat

When in doubt,,, Power out...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top