File permisson doesn't work

Hi,

I used below script to check the file permission but it doesnt work.

[root@sai4db1 tmp]# ls -ltr
total 52
-rwx------ 1 root root 1563 Mar  6 14:12 ks-script-t-WIE3
---x--x--x 1 root root  599 Mar  6 14:14 ks-script-t-WIE3.log
-rwx------ 1 root root  553 Mar  6 14:14 ks-script-P4gFC6
drwxrwxr-- 2 root root 4096 Mar  6 14:14 SAI-RHEL-SEC-patch
-rw-r--r-- 1 root root 1292 Mar  6 14:15 ks-script-P4gFC6.log
drwxr-xr-x 2 root root 4096 Mar  6 18:30 sai
-rwxr-xr-x 1 root root  147 Mar  7 13:56 test.sh
[root@sai4db1 tmp]# cat test.sh
#!/bin/bash

file="ks-script-t-WIE3.log"

if [ -r $file ]
then
echo "file has read permission"
else
echo "files doesnt have execute permission"
fi
[root@sai4db1 tmp]# ./test.sh
file has read permission
[root@sai4db1 tmp]#

Any idea??

if test -r astropubkey.asc ; then echo "file has r perms"; else echo "file is no r perms"; fi

Still same issue...Its strange really...

[root@sai4db1 tmp]# ll
total 52
-rwx------ 1 root root  553 Mar  6 14:14 ks-script-P4gFC6
-rw-r--r-- 1 root root 1292 Mar  6 14:15 ks-script-P4gFC6.log
-rwx------ 1 root root 1563 Mar  6 14:12 ks-script-t-WIE3
---x--x--x 1 root root  599 Mar  6 14:14 ks-script-t-WIE3.log
drwxr-xr-x 2 root root 4096 Mar  6 18:30 sai
drwxrwxr-- 2 root root 4096 Mar  6 14:14 SAI-RHEL-SEC-patch
-rwxr-xr-x 1 root root  148 Mar  7 14:30 test.sh
[root@sai4db1 tmp]# vim test.sh
[root@sai4db1 tmp]# cat test.sh
#!/bin/bash

file="ks-script-t-WIE3.log"

if test -r $file
then
echo "file has read permission"
else
echo "files doesnt have execute permission"
fi
[root@sai4db1 tmp]#

As I can infer from your prompt you are running that script as root. root may read everything. Try running as a normal user.

thanks for reply..
But using root user, it is working for +x checks but not for +r and +w. Any idea why?

Your line :

echo "files doesnt have execute permission"

should be replaced with :

echo "files doesnt have READ permission"

since you are testing against the "-r" option.

If you want to test against the execute permission, in your if test statement, you should use the "-x" option instead.

you are running as a root user, try changing ownership of the file to some other user and then test..

also, you are thinking for +x it is working correctly, but in fact check your code as ctsgnb mentioned.

That won't make a difference. As RudiC stated, if the shell is privileged, the read test will always succeed.

---------- Post updated at 03:00 PM ---------- Previous update was at 02:47 PM ----------

This is what lies behind bash's implementation, from lib/sh/eaccess.c:

  if (current_user.euid == 0)
    {
      /* Root can read or write any file. */
      if ((mode & X_OK) == 0)
	return (0);

      /* Root can execute any file that has any one of the execute
	 bits set. */
      if (st.st_mode & S_IXUGO)
	return (0);
    }

In that code, "mode" is an integral value representing what is being tested (reading = R_OK, writing = W_OK, executable = X_OK, a regular file = F_OK, etc).

Regards,
Alister