- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Conditional expressions ksh & posix sh
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2003 10:29 AM
04-25-2003 10:29 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2003 10:52 AM
04-25-2003 10:52 AM
Re: Conditional expressions ksh & posix sh
mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2003 11:02 AM
04-25-2003 11:02 AM
Re: Conditional expressions ksh & posix sh
$ 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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2003 11:06 AM
04-25-2003 11:06 AM
Re: Conditional expressions ksh & posix sh
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2003 11:12 AM
04-25-2003 11:12 AM
Re: Conditional expressions ksh & posix sh
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2003 11:14 AM
04-25-2003 11:14 AM
Re: Conditional expressions ksh & posix sh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2003 11:33 AM
04-25-2003 11:33 AM
Re: Conditional expressions ksh & posix sh
I get the same results with either ksh or sh.
Regards,
Scott Williams
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2003 11:49 AM
04-25-2003 11:49 AM
Solutiondirname...../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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2003 12:24 PM
04-25-2003 12:24 PM