1

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?

J.P. Armstrong
  • 836
  • 1
  • 9
  • 26
  • Why not just use `/usr/bin/ps` on Solaris? That's the version of ps that accepts the -ef flags. `/usr/ucb/ps` is an old wrapper for SunOS4/BSD compatibility. – alanc Nov 10 '13 at 19:09
  • @alanc In the end, it was another issue all together. I wasn't sure why they had `/usr/ucb/ps`. Thanks for clearing that up. – J.P. Armstrong Nov 11 '13 at 04:47

3 Answers3

5

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.
Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31
  • I'm trying to avoid changing this script since it's shared with other systems. Where is `ps` defined? Does it use the folders defined in the PATH? – J.P. Armstrong Nov 08 '13 at 19:41
  • _Which_ script is shared: the perl one? I thought you said you've modified it to use `ps_wrapper`... – Alexander L. Belikoff Nov 08 '13 at 19:43
  • The perl script is shared. the `ps_wrapper` is defined in my .profile. I want to trick the perl script into using the `ps` alias defined in the .profile. If perl respects the $PATH then an alternative is to override the ps bin. – J.P. Armstrong Nov 08 '13 at 19:49
  • 2
    Trust me, you *don't* want to override the system `ps` binary - you'll break more things than you care for. Instead, check the actual `PATH` used by the environment/user that will run your perl script and add another directory to it in front (or invoke your perl script with explicit `PATH`) to make sure it hits your `ps` instead of the system one. – Alexander L. Belikoff Nov 08 '13 at 20:01
1

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.

user2719058
  • 2,183
  • 11
  • 13
0

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
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
  • This is not a very sound idea. He may have arbitrary code in his `~/.bashrc`. – user2719058 Nov 08 '13 at 19:39
  • @user2719058: And? That is ok; if you want to use your aliases and they are described in the `~/.bashrc` there is no other way. – Igor Chubin Nov 08 '13 at 19:43
  • 1
    @user2719058: May be you better make up yours? I suppose that if you feel that is is a bad idea, it would be better to provide some arguments. Consider that fact that aliases are the part of interactive shell mode. And that's quite natural that they are used in combination with `~/.bashrc` – Igor Chubin Nov 09 '13 at 15:23