Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查当前用户是否可以在POSIX shell中写入文件_Posix_File Permissions_File Exists_File Ownership_Test - Fatal编程技术网

检查当前用户是否可以在POSIX shell中写入文件

检查当前用户是否可以在POSIX shell中写入文件,posix,file-permissions,file-exists,file-ownership,test,Posix,File Permissions,File Exists,File Ownership,Test,当前用户是$user。以前我使用: if [ -e "$f" ] && [ `stat -c %U "$f"` != $USER ] \ || [ -e "${f%/*}" ] && [ `stat -c %U "${f%/*}"` != $USER ]; then 检测文件是否为用户所有(如果文件尚不存在,则为dir) 但文件可以由另一个用户拥有,并且用户仍然可以修改它(组/其他可写) 如何检查当前用户可以通过POSIX实用程序(或者如果不可能,通过

当前用户是
$user
。以前我使用:

 if [ -e "$f" ] && [ `stat -c %U "$f"` != $USER ] \
    || [ -e "${f%/*}" ] && [ `stat -c %U "${f%/*}"` != $USER ]; then
检测文件是否为用户所有(如果文件尚不存在,则为dir)

但文件可以由另一个用户拥有,并且用户仍然可以修改它(组/其他可写)

如何检查当前用户可以通过POSIX实用程序(或者如果不可能,通过GNU扩展名)编辑该文件

我需要考虑扩展属性吗?如何处理它们?

似乎我忘记了实用性。为我感到羞耻

$ 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

目录也是一样。

Cant'你可以尝试在写模式下打开文件,检查打开的返回值,然后关闭文件描述符吗?我仅限于POSIX或GNU实用程序(命令行),无法编译自定义C程序。