Search
Enter Keywords:
Thursday, 02 September 2010

Running strerror() from the shell
Codemonkeying
Written by Dan   
Thursday, 05 February 2009 12:57

Reasonably often, I have a need to convert an errno variable to its string error message right at the shell.  This seems to come up relatively often in kernel development, but occasionally elsewhere.  Where I can, I usually just go for a printf("Error: %m\n") or printf("Error: %s\n", strerror(foo)) directly in the debug statement I'm adding.  However, %m doesn't work in the kernel and occasionally I either want or have to avoid recompiling something that just prints the integer value.  Chasing the header file chain to figure out what a given error code is can be time consuming and silly, but I've done it.

Recently, I've been trying to notice when I do stupid things repetitively and take the time to write a helper script or shell alias to prevent wasting effort the next time I need to do a particular task.  I have a collection of shell aliases, but I also keep a ~/bin directory with helper scripts for things that fit that mold better.  To solve my errno issue, I wrote the following today and stored it in ~/bin/perror:

#!/usr/bin/python

import os
import sys

for i in sys.argv[1:]:
    i = abs(int(i))

    print "%3i: %s" % (i, os.strerror(i))

It could probably be done smaller in a shell alias that calls perl or something, but oh well.  This is pretty handy so I can quickly get a couple of values:

% perror 111 22
111: Connection refused
 22: Invalid argument

I've already used it several times today, so it was definitely worth the 2 minutes it took to codify it.

Comments
Add New Search RSS
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
Please input the anti-spam code that you can read in the image.
 
© 2010 danplanet
Joomla! is Free Software released under the GNU General Public License.