1

Current user is $USER. Previously I use:

 if [ -e "$f" ] && [ `stat -c %U "$f"` != $USER ] \
    || [ -e "${f%/*}" ] && [ `stat -c %U "${f%/*}"` != $USER ]; then

to detect if file is owned by user (or dir if file is not yet exist).

But file can be owned by another and user still be able to modify it (group/other writable).

How can I check that file can be edited by current user by POSIX utilities (or if not possible - by GNU extensions)?

Do I need take into consideration extended attributes? How to deal with them?

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • Cant' you just try to open the file in write mode, check the return value of open and then close the file descriptor ? – Claudio Dec 10 '15 at 10:50
  • I am restricted to POSIX or GNU utilities (command line) and have no ability to compile custom C program. – gavenkoa Dec 10 '15 at 10:53

1 Answers1

1

Seems that I forget about utility. Shame for me!

$ test -w /etc/hostname && echo ok || echo fail
fail
$ [ -w /etc/hostname ] && echo ok || echo fail
fail
$ sudo test -w /etc/hostname && echo ok || echo fail
ok
$ sudo [ -w /etc/hostname ] && echo ok || echo fail
ok

Same for directory.

gavenkoa
  • 45,285
  • 19
  • 251
  • 303