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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

test pattern in [[]] 2

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL
hello,
is the test with [[ ... && .... ]] the only way here? how should look test with "-a" [ .... -a .....] in case of pattern test + file test as in example below?

Code:
$ A=/tmp/abcd
$ [[ "${A}" = /tmp/* ]] && echo pat ok || echo pat not ok
pat ok
$ A=/tm/abcd
$ [[ "${A}" = /tmp/* ]] && echo pat ok || echo pat not ok
pat not ok
$ A=/tmp/abcd
$ ls /tmp/abcd
ls: 0653-341 The file /tmp/abcd does not exist.
$ [[ "${A}" = /tmp/* [COLOR=#CC0000]-a[/color] ! -e "${A}" ]] && echo pat ok and file not exists  || echo pat not ok and/or file exists
ksh: 0403-057 Syntax error: `-a' is not expected.     [COLOR=#CC0000]<--- error[/color]
$ [[ "${A}" = /tmp/* [COLOR=#CC0000]&[/color] ! -e "${A}" ]] && echo pat ok and file not exists  || echo pat not ok and/or file exists
ksh: 0403-057 Syntax error: `&' is not expected.     [COLOR=#CC0000]<--- error[/color]
$ [[ "${A}" = /tmp/* [COLOR=#CC0000]&&[/color] ! -e "${A}" ]] && echo pat ok and file not exists  || echo pat not ok and/or file exists
pat ok and file not exists     [COLOR=#CC0000]<--- ok[/color]
$ touch /tmp/abcd
$ [[ "${A}" = /tmp/* && ! -e "${A}" ]] && echo pat ok and file not exists  || echo pat not ok and/or file exists
pat not ok and/or file exists     [COLOR=#CC0000]<--- error[/color]
 

of course last command is also ok (mistakenly marked it with red error)
 
is the test with [[ ... && .... ]] the only way here?

The 'only way' for what??

You have not explained what it is you are actually trying to do here.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Anyway: man ksh

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The Korn shell has a pattern match operator "[tt]@(pattern)[/tt]". Use it like this...

Code:
[[ $A = @(/tmp/*) && -e $A ]] && print Yes || print No

Also, the single ampersand "&" is a bitwise AND. The double ampersand "&&" is a boolean AND.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top