Operating System - HP-UX
1833996 Members
2160 Online
110063 Solutions
New Discussion

Conditional expressions ksh & posix sh

 
SOLVED
Go to solution
Scott Williams_5
Frequent Advisor

Conditional expressions ksh & posix sh

I've attached a simple posix script that performs different formats of "if" statements to determine if a file and a directory exist.

First I test for the existance (i.e. -f for files & -d for direcotries..all should exist), and then I test for non-existance displaying the results (see below).

Both the directory and the files I'm looking for do exist, however, the "if" statements are are not evaluated the same.

Can someone tell me what's going on and what the preferred method is for testing file/directory attributes?

Here's the output:
ux10-lacv-swilliam-/work01/tmp/work >./x >x.output
ux10-lacv-swilliam-/work01/tmp/work >ll
total 10
-rwx------ 1 swilliam lawson 2078 Apr 25 13:27 x
-rw-rw---- 1 swilliam lawson 1211 Apr 25 13:27 x.output
ux10-lacv-swilliam-/work01/tmp/work >cat x.output
dirname...../work01/tmp
filemask....11*
filename..../work01/tmp/11*
-rw-rw---- 1 swilliam lawson 544355 Apr 25 10:24 /work01/tmp/11425039.txt
-rw-rw---- 1 swilliam lawson 509776 Apr 25 10:24 /work01/tmp/11425040.txt
-rw-rw---- 1 swilliam lawson 471271 Apr 25 10:24 /work01/tmp/11425041.txt
-rw-rw---- 1 swilliam lawson 501018 Apr 25 10:24 /work01/tmp/11425042.txt
=========================================================
Doing Directory Tests

1)Testing: if [[ ( -d /work01/tmp ) ]] >Evaluates as true
2)Testing: if [[ ! ( -d /work01/tmp ) ]] >Evaluates as false
3)Testing: if [[ -d /work01/tmp ]] >Evaluates as true
4)Testing: if [[ ! -d /work01/tmp ]] >Evaluates as false
5)Testing: if [ -d /work01/tmp ] >Evaluates as true
6)Testing: if [ ! -d /work01/tmp ] >Evaluates as false
Doing File tests

1)Testing: if [[ ( -f /work01/tmp/11* ) ]] >Evaluates as false
2)Testing: if [[ ! ( -f /work01/tmp/11* ) ]] >Evaluates as true
3)Testing: if [[ -f /work01/tmp/11* ]] >Evaluates as false
4)Testing: if [[ ! -f /work01/tmp/11* ]] >Evaluates as true
5)Testing: if [ -f /work01/tmp/11* ] >Evaluates as true
6)Testing: if [ ! -f /work01/tmp/11* ] >Evaluates as false



Thanks so much

8 REPLIES 8
Mark Greene_1
Honored Contributor

Re: Conditional expressions ksh & posix sh

The only major difference is the use of the "!" within the expression. Considering that all that "!" does is flip the result bit (from true to false, or false to true), all appears to be working as advertised, unless I am missing something (which is entirely possible).

mark
the future will be a lot like now, only later
S.K. Chan
Honored Contributor

Re: Conditional expressions ksh & posix sh

One method that I like to use (especially if you are using wildcards) is the "test" command. Do ..
$ man test
for details. And from your example you can code it like so ..
..
if test -d ${dirname}
..
if test ! -d ${dirname}
..

and so on for the files as well. That takes away having to deal with square brackets, round brackets, etc (for your case).
Scott Williams_5
Frequent Advisor

Re: Conditional expressions ksh & posix sh

Mark, the directory tests work as expected;the file tests do not. Only version 5 & 6 evaluate as they should.

Thanks
Scott Williams_5
Frequent Advisor

Re: Conditional expressions ksh & posix sh

S.K.Chan,

My experience has been that it's only file object tests that behave oddly. Requires the single bracket: [ ] to be evaluated properly. It just doesn't make sense to me!

Well, thanks for the reccomendation...looks like I've got a lot of old code to touch-up!

Regards
Scott Williams
Tom Maloy
Respected Contributor

Re: Conditional expressions ksh & posix sh

If I check "man sh" versus "man ksh", only ksh shows "[[". Try running with ksh instead of sh.
Carpe diem!
Scott Williams_5
Frequent Advisor

Re: Conditional expressions ksh & posix sh

Tom,

I get the same results with either ksh or sh.

Regards,
Scott Williams
Tom Maloy
Respected Contributor
Solution

Re: Conditional expressions ksh & posix sh

You are testing just fine. It is not the test, it is the wildcard (*) in the filename. If you add a "*" to the directory name, you will get the same bad behavior.

dirname...../tmp/tpm*
filemask....k*
filename..../tmp/tpm*/k*
-rw-r--r-- 1 root sys 2065 Apr 25 15:36 /tmp/tpm/k
=========================================================
Doing Directory Tests

1)Testing: if [[ ( -d /tmp/tpm* ) ]] >Evaluates as false
2)Testing: if [[ ! ( -d /tmp/tpm* ) ]] >Evaluates as true
3)Testing: if [[ -d /tmp/tpm* ]] >Evaluates as false
4)Testing: if [[ ! -d /tmp/tpm* ]] >Evaluates as true
5)Testing: if [ -d /tmp/tpm* ] >Evaluates as true
6)Testing: if [ ! -d /tmp/tpm* ] >Evaluates as false
Doing File tests

1)Testing: if [[ ( -f /tmp/tpm*/k* ) ]] >Evaluates as false
2)Testing: if [[ ! ( -f /tmp/tpm*/k* ) ]] >Evaluates as true
3)Testing: if [[ -f /tmp/tpm*/k* ]] >Evaluates as false
4)Testing: if [[ ! -f /tmp/tpm*/k* ]] >Evaluates as true
5)Testing: if [ -f /tmp/tpm*/k* ] >Evaluates as true
6)Testing: if [ ! -f /tmp/tpm*/k* ] >Evaluates as false


And from ksh's man page:

A conditional expression is used with the [[ compound command to test
attributes of files and to compare strings. Word splitting and file
name generation are not performed on the words between [[ and ]].

So using [[ and ]] should only be done withOUT wildcards.

You could put it in a loop:

for i in $filename
do
#test here
done

If you need to keep the wildcards.

Carpe diem!
Scott Williams_5
Frequent Advisor

Re: Conditional expressions ksh & posix sh

Aaahh, enlightenment is a beautiful thing! Tom, I really appreciate it. Thanks so much.