Linux.com

Home Linux Community Forums New to Linux Getting Started with Linux Facing problem in testing the file permission

Facing problem in testing the file permission

Link to this post 22 Jan 10

script name: filetest.sh

if [ $# -eq 0 ] ; then
echo " You didn't enter any argument"
elif [ ! -e $1 ] ; then
echo " file not exist"
elif [ ! -r $1 ] ; then
echo " file not readable"
elif [ ! -w $1 ] ; then
echo " file not writable"
else
echo " file both readable and writable"
fi


running like...

$ ./filetest filename

but it showing.....both readable and writable

for those file which are not readable and writable...

Link to this post 22 Jan 10

The problem is how you are checking for an empty string, this is what is causing the problems, you want to use [ -z $1 ] which check is the string is zero length.
A working form of the script is below.

if [ -z $1 ] ; then
echo " You didn't enter any argument"
elif [ ! -e $1 ] ; then
echo " file not exist"
elif [ ! -r $1 ] ; then
echo " file not readable"
elif [ ! -w $1 ] ; then
echo " file not writable"
else
echo " file both readable and writable"
fi

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board