unix – 如何区分“No such file or directory”和“Permission Denied”之间的区别

前端之家收集整理的这篇文章主要介绍了unix – 如何区分“No such file or directory”和“Permission Denied”之间的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我宁愿不解析STDERR,但我想不出另外一种方法来区分两者之间的差异:
$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中尝试过各种文件测试,但它们都返回相同的代码.

改为测试文件.
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

有关其他可能性,请参见测试手册页.

猜你在找的Bash相关文章