如何区分“没有这样的文件或目录”和“权限被拒绝”

wsa*_*ton 3 unix file-permissions

我宁愿不解析 STDERR,但我想不出另一种方法来以编程方式区分两者之间的区别:

$ ls /net/foo.example.com/bar/test
/net/foo.example.com/bar/test: Permission denied
$ ls /sdfsdf
/sdfsdf: No such file or directory
Run Code Online (Sandbox Code Playgroud)

无论我尝试什么命令,它们似乎都返回相同的错误代码,所以这是一个死胡同:

$ ls /net/foo.example.com/bar/test
/net/foo.example.com/bar/test: Permission denied
$ echo $?
2
$ ls /sdfsdf
/sdfsdf: No such file or directory
$ echo $?
2
Run Code Online (Sandbox Code Playgroud)

我在 perl 中尝试了各种文件测试,但它们都返回相同的代码。

Mic*_*ton 8

改为测试文件。

test -e /etc/shadow && echo The file is there

test -f /etc/shadow && echo The file is a file

test -d /etc/shadow && echo Oops, that file is a directory

test -r /etc/shadow && echo I can read the file

test -w /etc/shadow && echo I can write the file
Run Code Online (Sandbox Code Playgroud)

有关test其他可能性,请参阅手册页。

  • 它是如何“不起作用”的?你甚至可以访问共享吗? (2认同)