Unix Shell - directory test

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > directory test





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author directory test
Rob Bradford

2006-01-20, 6:03 pm

I'm trying to test the existance of a directory using -f option of the test
command (bourne shell). No matter how I phrase it $? always returns false
even if the directory exists. (HP-UX 11i)

Any help would be appreciated.


Ed Morton

2006-01-20, 6:03 pm

Rob Bradford wrote:
> I'm trying to test the existance of a directory using -f option of the test
> command (bourne shell). No matter how I phrase it $? always returns false
> even if the directory exists. (HP-UX 11i)
>
> Any help would be appreciated.
>
>


use -d (for directory), not -f (for file).

Ed.
Rodrick Brown

2006-01-21, 2:49 am


"Rob Bradford" <rob.bradford@lineone.net> wrote in message
news:dqrfvc$5ab$1@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
> I'm trying to test the existance of a directory using -f option of the
> test command (bourne shell). No matter how I phrase it $? always returns
> false even if the directory exists. (HP-UX 11i)
>
> Any help would be appreciated.
>


$ [[ -d / ]] && echo $?
0
$


jhding@tom.com

2006-01-23, 2:55 am

Rodrick Brown wrote:
> "Rob Bradford" <rob.bradford@lineone.net> wrote in message
> news:dqrfvc$5ab$1@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
>
> $ [[ -d / ]] && echo $?
> 0
> $

I perfer the following command:
<cmd>for f in `find . -name "*.c"`; do basename $f; done</cmd>
In fact, you can do even more processing with $f if you like, such as:
<cmd>for f in `find . -name "*.c"`; do basename $f; dirname $f;
done</cmd>

Good luck!

jhding@tom.com

2006-01-23, 2:55 am

<cmd> if [[ -d / ]]; then echo $? hello; fi</cmd>
will return <stdout>0 hello</stdout>.

Funny? 0 means true, 1 means false. )
Try the following commands if you were not able to believe it:
<cmd> true && echo $?</cmd>
<cmd>false || echo $?</cmd>

Caution: Csh is different. Csh is more like C, but sh and bash are not.

Stephane CHAZELAS

2006-01-23, 2:55 am

2006-01-22, 20:31(-08), jhding@tom.com:
> <cmd> if [[ -d / ]]; then echo $? hello; fi</cmd>
> will return <stdout>0 hello</stdout>.
> Funny? 0 means true, 1 means false. )


No, 0 means OK, anything else means an error, the value of $?
may give an indication of the value of the error.

> Try the following commands if you were not able to believe it:
> <cmd> true && echo $?</cmd>
> <cmd>false || echo $?</cmd>
>
> Caution: Csh is different. Csh is more like C, but sh and bash are not.


No, it's all the commands that have that logic, if csh behaved
otherwise, it would be definitely broken an unusable.

$ csh -c 'true && echo $status'
0
$ csh -c 'false || echo $status'
1


--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com