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

How do I group the test operators

Status
Not open for further replies.

meetramana

Programmer
Feb 2, 2005
57
US
Hi,

How do I group the test operators with out making it a very long expression

For example
I was trying to do somethings like

if [ -(afr) $FILENAME ]; then
echo "FILE EXISTS"
else
echo "FILE EITHER DOES NOT EXIST OR NOT A REGUALR FILE OR NOT READABLE"
fi

Thanks

 
I don't think test likes grouping flags like this. At least on AIX it doesn't work.

If you are worried about readability you could always put a longish combination of the test flags in a function:

# # #
# test if a file ($1) is an ordinary file
# and
# if it is readable by this process
test_fr ()
{
if [ -f $1 -a -r $1 ]
then
return 0
else
return 1
fi
}

then in your script code:

...
if test_fr ${FILENAME}
then
...

Granted, like this you need a function for every combination of flags you are interested in.


I guess you could write a more intelligent version of this test function which takes flags like you suggested and turns this into a real -a'd or -o'd combination of flags/argument that test understands. I'll leave that up to you.


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top