Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Gathering current ksh shell aliases in perl

by kreon3 (Novice)
on Apr 26, 2010 at 20:55 UTC ( [id://836980]=perlquestion: print w/replies, xml ) Need Help??

kreon3 has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a script that uses the aliases of the current ksh shell and processes them. I am running into an issue where any execution (system, ``, open piping) of the "alias" command creates a new shell instance and does not gather my current aliases. I have tried a variety of approaches so far from the simple ways:

my $alias_str = `alias`;
my $alias_str_2 = `. alias`;
my $res = qx(alias);
open (PIPE, "alias |");

To even complex methods:

system "echo \"alias > aliases.tmp\" > get_aliases.ksh"; chmod (0777, 'get_aliases.ksh'); system '. get_aliases.ksh'; open (ALIASES, "aliases.tmp"); foreach my $line (<ALIASES>){ print "alias line $line\n"; }

Nothing appears to be working because perl or ksh creates a new shell instance preventing gathering of the current shell's aliases.

Anyone have any ideas on how to pull the ksh aliases into perl? Thanks.

Replies are listed 'Best First'.
Re: Gathering current ksh shell aliases in perl
by ikegami (Patriarch) on Apr 26, 2010 at 21:15 UTC
    You can't get information from a process unless it provides a means of doing so, and I'd be surprised if ksh processes provide a means for other process to query their aliases. You'll have to pass the information to Perl.
    alias | foo.pl
Re: Gathering current ksh shell aliases in perl
by JavaFan (Canon) on Apr 26, 2010 at 21:49 UTC
    Unix OS don't have much of an API to introspect another process - and certainly not not for things that are very program specific. Just as ikegami said, if the parent process doesn't tell, there's no way to retrieve that bit of information. (Well, I guess that if your program runs as root, you may be able to poke around in a process memory - given enough knowledge of the internals of the version of your ksh, you could in theory reconstruct the aliases. But I wouldn't recommend that.)

      Just out of curiosity, what operating systems do have that kind of API?

      I vaguely recall Plan 9 having some level of FS type access to process internal state, but I may be pulling that out of the ether.

      for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
        Some OSses have an API to get some introspection about processes. Think SUNs Dtrace, or HPs Glance. I guess one could even consider /proc to be an API to get some introspection.

        But I don't know of an OS that allows querying for aliases.

Re: Gathering current ksh shell aliases in perl
by cdarke (Prior) on Apr 27, 2010 at 11:31 UTC
    use warnings; use strict; my @aliases = qx(ksh -c alias); print "@aliases\n";
    However, this with create a non-login shell. What happens now depends on the versions of ksh you are using, and where your aliases are defined. Let me assume your aliases are in .kshrc. If so, then on ksh88 then ~/.kshrc (or the file defined in the ENV environment variable) will be executed and you should be fine. However on ksh93 it will not execute $ENV, so you need:
    my @aliases = qx(ksh -c '. ~/.kshrc;alias');
    or 'source' whatever file your aliases are defined in.

      But that still does not return the aliases defined interactively in the ksh instance invoking the perl script.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Gathering current ksh shell aliases in perl
by samarzone (Pilgrim) on Apr 27, 2010 at 07:56 UTC

    Update:While submitting this answer I assume that you are expecting the command ("alias"), written within backticks, to run like it does inside a shell

    Read perldoc for system

    perldoc -f system

    It says -

    ...If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system’s command shell for parsing (this is "/bin/sh -c" on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly to "execvp",...

    Check the return value of system or open. They indicate errors.

    # perl -e 'print system("alias")' -1 # perl -e 'open (PIPE, "alias |") or die $@;' Died at -e line 1.

    When you do system("alias") perl searches for an executable file named alias which, as expected, is not found

    type alias alias is a shell builtin
    I don't know but I think backtick operators (``) and open should also work execute commands like system

    The things I want to say are

    1. when you do perl -e '$output = `alias`' the shell is nowhere in the picture.
    2. you can not grab output of builtin shell commands
    3. you can grab output of only those commands which can be run in following manner # sh -c "cmd args"

    ------

    Kind monks! Please correct me if I am wrong

Re: Gathering current ksh shell aliases in perl
by LanX (Saint) on Apr 28, 2010 at 07:55 UTC
    Hi

    I'm not sure what the aliases of the current ksh shell exactly means...

    ...here two pragmatic solutions:

    a) if you wanna process the saved aliases of your .aliases do it like cdarke showed, since alias is a builtin of various shells and not an isolated unix command.

    b) if you need to process the interactively created aliases from the running ksh, why don't you simply bind a new alias to call your script with the aliases piped in?

    this works for me in bash:

    lanx@nc10-ubuntu:~$ alias plalias='alias |perl -pe "s/alias//;print $. +"' lanx@nc10-ubuntu:~$ plalias 1 egrep='egrep --color=auto' ... 11 ls='ls --color=auto' 12 plalias='alias |perl -pe "s/alias//;print $."'

    of course you have to process a new alias called plalias now... :)

    HTH!

    Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://836980]
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-29 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found