AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 597325
Accepted
wsaxton
wsaxton
Asked: 2014-05-21 12:14:12 +0800 CST2014-05-21 12:14:12 +0800 CST 2014-05-21 12:14:12 +0800 CST

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

  • 772

我宁愿不解析 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 中尝试了各种文件测试,但它们都返回相同的代码。

unix
  • 3 3 个回答
  • 2081 Views

3 个回答

  • Voted
  1. Best Answer
    Michael Hampton
    2014-05-21T12:16:35+08:002014-05-21T12:16:35+08:00

    而是测试文件。

    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手册页。

    • 9
  2. ceejayoz
    2014-05-21T12:16:27+08:002014-05-21T12:16:27+08:00
    $ test -f /etc/passwd
    $ echo $?
    0
    
    $ test -f /etc/passwds
    $ echo $?
    1
    
    • 1
  3. jrw32982
    2021-12-16T16:00:59+08:002021-12-16T16:00:59+08:00

    其他答案并没有真正区分不同的情况,但是这个perl脚本可以:

    $ cat testscript
    chk() {
       perl -MErrno=ENOENT,EACCES -e '
          exit 0 if -e shift;        # exists
          exit 2 if $! == ENOENT;    # no such file/dir
          exit 3 if $! == EACCES;    # permission denied
          exit 1;                    # other error
       ' -- "$1"
       printf "%s %s  " "$?" "$1"
       [[ -e "$1" ]] && echo "(exists)" || echo "(DOES NOT EXIST)"
    }
    chk /root
    chk /etc/passwd/blah
    chk /x/y/z
    chk /xyz
    chk /root/.profile
    chk /root/x/y/z
    
    $ ./testscript
    0 /root  (exists)
    1 /etc/passwd/blah  (DOES NOT EXIST)
    2 /x/y/z  (DOES NOT EXIST)
    2 /xyz  (DOES NOT EXIST)
    3 /root/.profile  (DOES NOT EXIST)
    3 /root/x/y/z  (DOES NOT EXIST)
    

    有关可能的错误代码,请参见stat(2)手册页。

    • 1

相关问题

  • Windows 有符号链接吗?

  • 控制 UNIX 目录内容用户组所有权

  • 在命令行上从 csv 文件中过滤带有空格字符的字段

  • 如何将 VAR 从子 shell 导出到父 shell?

  • 查找文件大小(以 MB 为单位)

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve