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 / 问题 / 38582
In Process
An̲̳̳drew
An̲̳̳drew
Asked: 2009-07-10 11:59:19 +0800 CST2009-07-10 11:59:19 +0800 CST 2009-07-10 11:59:19 +0800 CST

如何将错误号转换为“errno”常量?

  • 772

假设我有一个在 UNIX 机器上运行的应用程序失败,系统错误状态为“13”。现在,我可以很容易地在 errno.h 中查找这个值,发现这是一个权限被拒绝的问题。

> grep -w 13 /usr/include/errno.h
#define EACCES  13      /* Permission denied                    */

有没有更简单的命令来检索这些信息?我希望能够运行这样的东西:

> lookuperror 13
EACCES (Permission denied)

而不是 grepping 系统头文件。这样的命令/程序是否存在?

更新: 正如下面的答案所指出的,strerror()系统调用会返回此信息。是否有任何 UNIX 操作系统附带执行此系统调用的可执行实用程序,还是我需要编写自己的程序来执行此操作?

unix troubleshooting
  • 5 5 个回答
  • 3958 Views

5 个回答

  • Voted
  1. radius
    2009-07-10T12:09:57+08:002009-07-10T12:09:57+08:00

    我用来做

    perl -MPOSIX -e 'print strerror($ARGV[0])."\n";' 13
    

    您可以将 Perl 代码放在一个文件中并将其放在路径中。
    当然也可以用C来完成

    • 8
  2. Juliano
    2009-07-10T12:22:23+08:002009-07-10T12:22:23+08:00
    ~% perror 13
    OS error code  13:  Permission denied
    ~% rpm -qf =perror
    mysql-server-5.0.45-7.el5
    
    • 4
  3. kurin
    2009-07-10T12:11:01+08:002009-07-10T12:11:01+08:00

    试试 strerror(3)。

    从手册页:

    DESCRIPTION
    
     The strerror(), strerror_r() and perror() functions look up the error
     message string corresponding to an error number.
    
     The strerror() function accepts an error number argument errnum and
     returns a pointer to the corresponding message string.
    
     The strerror_r() function renders the same result into strerrbuf for a
     maximum of buflen characters and returns 0 upon success.
    
     The perror() function finds the error message corresponding to the cur-
     rent value of the global variable errno (intro(2)) and writes it, fol-
     lowed by a newline, to the standard error file descriptor.  If the argu-
     ment string is non-NULL and does not point to the null character, this
     string is prepended to the message string and separated from it by a
     colon and space (``: ''); otherwise, only the error message string is
     printed.
    
     If the error number is not recognized, these functions return an error
     message string containing ``Unknown error: '' followed by the error num-
     ber in decimal.  The strerror() and strerror_r() functions return EINVAL
     as a warning.  Error numbers recognized by this implementation fall in
     the range 0 < errnum < sys_nerr.
    
     If insufficient storage is provided in strerrbuf (as specified in buflen)
     to contain the error string, strerror_r() returns ERANGE and strerrbuf
     will contain an error message that has been truncated and NUL terminated
     to fit the length specified by buflen.
    
     The message strings can be accessed directly using the external array
     sys_errlist.  The external value sys_nerr contains a count of the mes-
     sages in sys_errlist.  The use of these variables is deprecated;
     strerror() or strerror_r() should be used instead.
    
    • 2
  4. Chealion
    2009-07-10T12:06:13+08:002009-07-10T12:06:13+08:00

    作为一种解决方法,您可以在 shell 中创建别名或函数:

    例如。.bashrc

    function lookuperror
    {
        grep -w "$@" /usr/include/errno.h
    }
    
    • 1
  5. Josh Kelley
    2009-07-10T12:44:00+08:002009-07-10T12:44:00+08:00

    cpp -dM预处理源文件或头文件并打印#define它找到的每一个。它比 grepping through 更强大/usr/include/errno.h,因为它会获取/usr/include/errno.h包含的每个文件。

    将 cpp -dM 与其他人的建议相结合:

    function lookuperror
    {
        cpp -dM /usr/include/errno.h | grep -w "$@"
        perl -MPOSIX -e 'print "Description:".strerror($ARGV[0])."\n";' $@
    }
    

    插入 .bashrc,或将其内容作为独立的 shell 脚本。

    • 1

相关问题

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

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

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

  • 如何及时了解有关 UNIX 软件补丁和升级的最新信息?

  • 您为服务器使用和推荐哪种 UNIX 文件系统?

Sidebar

Stats

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

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

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

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

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

    • 3 个回答
  • Marko Smith

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

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +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