Someone asked an interesting question in comp.lang.perl.tk today, about how to get a list of all bash shell aliases for the current user.

It went like this:

n my perl script, I need to know the command alias name to make my program flexible. I tried many methods like system("alias cmd"), `alias cmd`, use shell qw,etc. But no one could recognize the built in command alias. If I use `which cmd`, it would look for the command based on environmental PATH and skip the alias information. Finally, I found a useful document from Shell - perldoc.perl.org. It says "It isn't possible to call shell built in commands, but it can be done by using a workaround, e.g. shell( '-c', 'set' )." Does anyone know how to implement the work around method "shell( '-c', 'set' )" or any possible method to get command alias information? I'm really exhausted for this problem. Please give me a hand.

Well I started to play around, and found a way, but I was wondering if this is the best that can be done. The basic problem is that when running the IPC, the OUT filehandle would never close, requiring me to wrap the while (sysread, OUT, 512) loop in an eval to time it out. Otherwise, the script will just sit in the loop until you control-c. This hack is pretty bad, involving a goto and an eval, but it works. Is there any other way to break out of the hanging sysread?

#!/usr/bin/perl use warnings; use strict; use IPC::Open3; $|=1; my $pid=open3(\*IN,\*OUT,\*ERR,'/bin/bash -i'); my $cmd = 'builtin alias'; #send cmd to bash print IN "$cmd\n"; my $result_err = <ERR>; print "ERR->$result_err\n"; my @aliases; my $buf; my $timeout = 1; #after 1 seconds no bash output, script continues eval{ local $SIG{ALRM} = sub { goto END }; alarm $timeout; while( sysread(OUT, $buf, 512)){ #print $buf; push @aliases,$buf; } alarm 0; }; END: print "@aliases\n";

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to getting a list of your bash aliases by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.