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";
In reply to getting a list of your bash aliases by zentara
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |