[olug] Odd results from /bin/sh (GNU bash) and the "test" command...

Dan Linder dan at linder.org
Thu Feb 25 17:38:46 UTC 2010


I was debugging a script (CentOS 5.4 with all updated patches) that
was spewing errors to the screen.  The errors came from a simple "cat
/path/filename" and cat was reporting "Permission denied", and the
file had only a write permission:

# ls -al /sys/bus/pci/drivers_probe
--w------- 1 root root 4096 Feb 25 10:05 /sys/bus/pci/drivers_probe

The "test" (or [ ]) command has the -r option:
       -r FILE
              FILE exists and read permission is granted

But a quick test shows that "-r" is saying this file is readable:

#!/bin/sh
set -x
FILE=/tmp/notreadable
touch $FILE
chmod a-rwx $FILE
ls -al $FILE
if [ -r $FILE ] ; then
    echo Readable test 1
else
    echo NOT Readable test 1
fi

# /tmp/testread
+ FILE=/tmp/notreadable
+ touch /tmp/notreadable
+ chmod a-rwx /tmp/notreadable
+ ls -al /tmp/notreadable
---------- 1 root root 0 Feb 25 11:04 /tmp/notreadable
+ '[' -r /tmp/notreadable ']'
+ echo Readable test 1
Readable test 1

I haven't found if this is a bug or an odd 'quirk', but it happens as
both a normal user and as root.

If I'm using it incorrectly please tell me, or keep this in the back
of your mind if you have an odd script action...

Very weird...still investigating it.

Dan

-- 
***************** ************* *********** ******* ***** *** **
"Quis custodiet ipsos custodes?"
    (Who can watch the watchmen?)
    -- from the Satires of Juvenal
"I do not fear computers, I fear the lack of them."
    -- Isaac Asimov (Author)
** *** ***** ******* *********** ************* *****************



More information about the OLUG mailing list