Operating System - HP-UX
1838380 Members
2936 Online
110125 Solutions
New Discussion

How come my alias cannot work?

 
Rashid Ali
Frequent Advisor

How come my alias cannot work?

I worte an alias command as below,
alias psg='ps -el|grep \!*|grep -v grep'
But when I run "alias swapper", it says
grep: can't open swapper

But ps -ef|grep swapper can produce results as below,
# ps -ef|grep swapper
root 0 0 0 Apr 3 ? 1:28 swapper
root 26497 26069 2 11:51:42 ttyp3 0:00 grep swapper

Anybody know what's the reason?

Thanks,


6 REPLIES 6
Bill Hassell
Honored Contributor

Re: How come my alias cannot work?

The alias command is useful for holding a lot of frequently typed commands. However, alias is not like a shell script where parameters after the name of the alias can be imbedded inside the alias (ie, the name of the desired process).

However, there is a much better and more reliable alias to use:

alias psg="UNIX95= /usr/bin/ps -C"

Now type the command: psg swapper
or better yet: psg sh

You will see that only the true basename of the command is located and the non-specific matching of grep is avoided. The UNIX95 setting is documented in the ps man page.


Bill Hassell, sysadmin
linuxfan
Honored Contributor

Re: How come my alias cannot work?

Hi Zhang,

you could try something like this

alias psg='"ps -el|grep -v grep |grep "

once you source your .kshrc or your env file, you should be able to run
psg swapper

If you want to grep case insensitive you could modify your alias to
alias psg="ps -el |grep -v grep |grep "

Actually there is another easier way, if you are looking for a process with a known basename you could set up another alias
alias psf="UNIX95= ps -flC "

now running "psf swapper" would give you the same output.
For more information about UNIX95 (look at the manpage for ps)

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Rashid Ali
Frequent Advisor

Re: How come my alias cannot work?

Yes, it sounds great. But I am still wondering why I can't use grep \!* and when I can use it. Because I see this example from a book "UNIX Hacks&Hints". Does the following command work on other flavours of UNIX such as Solaris or AIX?
alias psg='ps -el|grep \!*|grep -v grep'

Anybody knows how to interpret "grep \!*" and show me an example?

One more puzzle, What does it mean by "UNIX95" in your command and how does it work?
Sridhar Bhaskarla
Honored Contributor

Re: How come my alias cannot work?

Specifying the variable UNIX95 tells ps to behave like XPG4 using which we can generate desired reports like

UNIX95= ps -e -o "pcpu pid args" |sort -n

which will print percentage cpu of the processes in the reversed sorted order.

There are good explanations by Bill and other folks in this forum itself. You can try searching with UNIX95 search string.

Checkout manpage for ps to find out various options that can be used with XPG4 implementation of ps.

-Sri

PS: Please assign some points to the explanations you think are helpful to you. That will encourage people to participate more in the discussions.
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: How come my alias cannot work?

If you really want to use what you read in Unix hacks&hints you can do it. Only thing is there one missing grep in your alias. It should be

alias psg='ps -ef |grep \!* |grep -v grep|grep'

This is your command with an additional grep statement.

Your earlier command would be parsed as

ps -el|grep \!*|grep -v grep inetd

and obviously would result with the error message cannot open inetd.

And your grep \!* seems to get all the processes. When we specify \!, the shell will not try to match the files in the local directory.

However, as suggested by Bill and Ramesh, it is better to use XPG4 version of ps.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Robin Wakefield
Honored Contributor

Re: How come my alias cannot work?

Hi,

alias arguments only work in csh, i.e. (note there is no = sign):

# alias pgp 'ps -ef|grep \!*|grep -v grep'
# pgp swapper
root 0 0 0 Jul 1 ? 135:39 swapper

If you want to use arguments in ksh, you have to use shell functions, or fix your alias so that any argument you type works if it is tagged onto the end of the definition, i.e.

alias pgp="ps -ef|grep -v grep | grep "

Rgds, Robin.

btw, points assignments are a little overdue ;-)