J.P. Armstrong
J.P. Armstrong

Reputation: 836

How can I modify a system command used in Perl script with aliases?

I have a perl script that calls ps -ef somewhere in the code. This script works in Linux but not in Solaris 5.10. My work around is to define an alias in my .profile:

ps_wrapper()
{
  if [[ $1 = "-ef" ]]; then 
    /usr/ucb/ps auxwwww
  else
    /usr/ucb/ps $1
  fi
}
alias ps=ps_wrapper

The problem is the alias is not available in the perl script. How can I get the script to see this alias?

Upvotes: 1

Views: 972

Answers (3)

Alexander L. Belikoff
Alexander L. Belikoff

Reputation: 5721

ps_wrapper is an alias known to your shell only, not to programs run underneath it. There are many ways to do what you are trying to accomplish here:

  • make ps_wrapper a script and put it on a PATH
  • Check the OS in your Perl script via POSIX::uname() and form the ps command line accordingly.
  • Search CPAN for a module implementing portable ps functionality (this one, maybe?).

A couple more thoughts:

  • In Linux, PS_PERSONALITY environment variable can be used to set the ps "personality."
  • In Solaris, ps adheres to POSIX/XPG/SUS, so by sticking to SUS semantics for the command line of ps you can (in theory) get to the same command line and output on both systems.

Upvotes: 5

user2719058
user2719058

Reputation: 2233

The shell alias won't help you with the perl script. Just move the workaround into the perl script, and you're done. If you don't want to add hacks to the script, use something like the perl module Proc::ProcessTable, which (hopefully) works on your target platforms.

Upvotes: 1

Igor Chubin
Igor Chubin

Reputation: 64603

You can run bash -ic if you want to use aliases from your .bashrc. Example:

$ grep hello ~/.bashrc
alias hello='echo hello'
$ bash -ic 'hello'
hello

Upvotes: 0

Related Questions