wsaxton asked:
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.
My answer:
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.
View the full question and any other answers on Server Fault.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
The post How to tell the difference between "No such file or directory" and "Permission Denied" appeared first on Ringing Liberty.