大网数据广告活动
服务器运维问答

How to tell the difference between "No such file or directory" and "Permission Denied"

· 52CE 我爱测

I'd rather not parse STDERR, but I can't think of another way to tell the difference, programmatically, between the two: $ ls /net/foo.example.com/bar/test /net/foo.example.com/bar/test: Permission denied $ ls /sdfsdf /sdfsdf: No…

问题原文

原帖链接 · wsaxton · CC BY-SA 3.0

I'd rather not parse STDERR, but I can't think of another way to tell the difference, programmatically, between the two:

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

No matter what command I try, they both seem to return the same error code, so that's a dead-end:

$ 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

I've tried the various file tests in perl, but they both return the same codes as well.

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 3.0

Test the file instead.

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

See the test man page for other possibilities.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-05-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。