Symbolic errno to String

2020-07-03 09:15发布

Is there a command-line tool that will take a symbolic errno such as EINVAL and print the corresponding string, Invalid argument?

I would like to avoid having to find that EINVAL is value 22 on my system and then using$ perror 22.

Ideally I could write something like

$ errorcommand EINVAL
Invalid argument
$

标签: bash errno
11条回答
做自己的国王
2楼-- · 2020-07-03 09:56

This works on Ubuntu 9.04:

user@host:~$ grep EINVAL /usr/include/asm-generic/errno*.h
/usr/include/asm-generic/errno-base.h:#define   EINVAL      22  /* Invalid argument */

You can also try a Python script:

import errno
from os import strerror
from sys import argv
print strerror(errno.__dict__[argv[1]]
查看更多
\"骚年 ilove
3楼-- · 2020-07-03 10:01

The function

strerror()

Is possibly what you're looking for, but I don't know of a command that exposes that to any shell offhand.

MKS exposes the command line strerror

查看更多
Melony?
4楼-- · 2020-07-03 10:02

On my corporate box /usr/include wasn't available. So I put this portable simple solution (if you have Python) into my init files. You can torture it into a one-liner if you wish:

function strerror () {                                                         
    python -c "import os, errno; print(os.strerror(errno.$1))"                  
}                                                                              
查看更多
欢心
5楼-- · 2020-07-03 10:04

Tried

grep EINVAL /usr/include/sys/errno.h

and seen what comes back?

查看更多
Evening l夕情丶
6楼-- · 2020-07-03 10:05

There's no standard utility to do this. I believe your best bet is to write such a utility yourself. Use strerror() to print the associated error message.

查看更多
登录 后发表回答