If I create a file as an unprivileged user, and change the permissions mode to 400
, it's seen by that user as read-only, correctly:
$ touch somefile
$ chmod 400 somefile
$ [ -w somefile ] && echo rw || echo ro
ro
All is well.
But then root comes along:
# [ -w somefile ] && echo rw || echo ro
rw
What the heck? Sure, root can write to read-only files, but it shouldn't make a habit of it: Best Practice would tend to dictate that I should be able to test for the write permission bit, and if it's not, then it was set that way for a reason.
I guess I want to understand both why this is happening, and how can I get a false return code when testing a file that doesn't have the write bit set?
Best Answer
test -w
aka[ -w
doesn't check the file mode. It checks if it's writable. For root, it is.The way I would test would be to do a bitwise comparison against the output of
stat(1)
("%a
Access rights in octal").Note the subshell
$(...)
needs a0
prefixed so that the output ofstat
is interpreted as octal by(( ... ))
.