问题原文
原帖链接 · 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
