INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

test pattern in [[]]

test pattern in [[]]

(OP)
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/* -a ! -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.     <--- error
$ [[ "${A}" = /tmp/* & ! -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.     <--- error
$ [[ "${A}" = /tmp/* && ! -e "${A}" ]] && echo pat ok and file not exists  || echo pat not ok and/or file exists
pat ok and file not exists     <--- ok
$ 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     <--- error 

RE: test pattern in [[]]

(OP)

of course last command is also ok (mistakenly marked it with red error)

RE: test pattern in [[]]

Quote:

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

RE: test pattern in [[]]

The Korn shell has a pattern match operator "@(pattern)". 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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close