In a shell script, how do I easily and non-invasively test for write access to a file without actually attempting to modify the file?
I could parse the output of stat, but that seems really complex, and perhaps brittle, though I'm not sure how much stat output differs across implementations and time.
I could append to the end of the file and see if that succeeds, but that's potentially dangerous, for two reasons I can think of:
- I now have to remove the addition, and in case some other process writes to the file, this immediately becomes non-trivial as my line is no longer the last one.
- Any process reading the file may have arbitrary requirements on the contents of that file, and I may just have broken that application.
man testorman [– chaos Oct 06 '14 at 12:57type -a– Volker Siegel Oct 06 '14 at 13:40man testorman [will give you the manual for the stand-alone command.help testwill give you the documentation for the built-in. They are not the same thing. – alexis Oct 06 '14 at 16:23testuseseuidaccesswhich simply checks permission bits. Aren't there other factors (e.g. SELinux) that could prohibit write access? – zamnuts Oct 06 '14 at 18:39&&and||have equal precedence. They're evaluated from left to right. – Wildcard Mar 06 '16 at 10:01