[Discuss] test for *other* write permissions

Edward Ned Harvey (blu) blu at nedharvey.com
Thu Nov 15 17:38:43 EST 2012


I'm running a script as root, I'm going to parse a file.  I can easily test that the file is owned by root.  I want to test if the group or other write bit is set.

If using "find" you can test that a file is writable by yourself - but not test if it's writable by anyone else.

If using "stat" you can get the perms in drwxrwxrws format.  But the number of characters sometimes varies, based on whether or not it's a character special device, directory, softlink, etc.  Also, the leading or trailing blank character ' ' space may or may not be present for different implementations of stat.  So although it's definitely possible to find the "w" bit in the "g" and "o" fields...  I am hesitant to do this.  Also, even if I want to do this, it's not immediately obvious to me, how to extract the nth character out of a string in a bash script.

If using "stat" you can also get the perms in 755 format.  From here, I can easily do a bitwise & with 2, and I can easily find the world write bit.  But bash arithmetic doesn't view the number "755" in octal...  It operates decimal.  So I can't bitwise & with 16, or with 020.  I have to strip off the last character, and then bitwise 75 with & 2.  So once again, given my previous comments about leading and trailing spaces and stuff, plus, not knowing how to get the nth character of a string, I'm not really sure there's a good solution here.

So far, my best idea is to check if the file is owned by root:root, and then just check the World write bit.

People have better suggestions?



More information about the Discuss mailing list