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

我宁愿不parsingSTDERR,但我不能想出另一种方式来区分两者之间的区别:

$ ls /net/foo.example.com/bar/test /net/foo.example.com/bar/test: Permission denied $ ls /sdfsdf /sdfsdf: No such file or directory 

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

 $ 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 

我已经尝试了perl中的各种文件testing,但是它们都返回相同的代码。

testing文件,而不是。

 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 

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

 $ test -f /etc/passwd $ echo $? 0 $ test -f /etc/passwds $ echo $? 1