1826069 Members
3680 Online
109690 Solutions
New Discussion

alias in ksh

 
SOLVED
Go to solution
Kellogg Unix Team
Trusted Contributor

alias in ksh

I want to setup an alias 'psg' which, when given a parameter, would give me processes grep-ed by that parameter, viz.

psg oracle (would list all procs which has oracle word in them)

psg http (all procs with http word in them)

alias=`ps -ef|grep -i |grep -v grep'

I was able to do the above in csh by substituting with \!*

What is the corresponding string for ksh?

Thanks in advance
...Manjeet
work is fun ! (my manager is standing behind me!!)
14 REPLIES 14
Kellogg Unix Team
Trusted Contributor

Re: alias in ksh

alias psg=`ps ....`

Sorry for the typo!
work is fun ! (my manager is standing behind me!!)
Rodney Hills
Honored Contributor

Re: alias in ksh

Aliases aren't very smart in ksh. Better to use function.

example-
function psg {ps -ef | grep $1 | grep -v "grep"}

HTH

-- Rod Hills
There be dragons...
Jeff Schussele
Honored Contributor

Re: alias in ksh

Hi,

Should be

ps -ef | grep -i $1 | grep -v grep

$X where X is numeric are argument vars.
$0 = command
$1 = 1st argument
$2 = 2nd argument
etc.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Jeff Schussele
Honored Contributor

Re: alias in ksh

So you could even *extend* that alias command by simply defining it to be:

ps -ef | grep -i -e $1 -e $2 -e $3 ....etc | grep -v grep

And then you could do

psg oracle http java

and get all procs with any of those.

Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Bill Hassell
Honored Contributor

Re: alias in ksh

The easiest is to use a separate script rather than an alias as you could limit which fields will be used for the comparison. However, to find all processes owned by the oracle user, use ps options:

ps -f -u oracle

Or to search for a specific process (and not locate processes with unrelated strings), use the -C option:

UNIX95= ps -f -C sh

which will NOT find ksh like grep does.


Bill Hassell, sysadmin
monasingh_1
Trusted Contributor

Re: alias in ksh

just a note while setting alias in ksh you should give ' (forward slash) not the ` (back slash).

I do not know how to replace your ??? of csh but why not just do ps instead of writting a function. I think ps is given for this purpose.

Use as many standard commands as you can before writting your own functions, otherwise it will only complicate things further.

hope this helps..
Kellogg Unix Team
Trusted Contributor

Re: alias in ksh

Rodney,

How would I make use of this function? Do I include it in a script?

psg is just an example; I have many aliases defined that way in my .cshrc. That will mean writing scripts for all of them!

Thanks
work is fun ! (my manager is standing behind me!!)
Kellogg Unix Team
Trusted Contributor

Re: alias in ksh

Jeff S.

$1 doesn't work in alias.

Thanks
work is fun ! (my manager is standing behind me!!)
Kellogg Unix Team
Trusted Contributor

Re: alias in ksh

Bill H.

Its not a single alias that I am trying to work. I have lot many and writing scripts for them may be an overkill. But so far, what I am learning from user-community is that ksh doesn't have that feature.

Thanks for the tip for -C flag. How would I use it?
work is fun ! (my manager is standing behind me!!)
Kellogg Unix Team
Trusted Contributor

Re: alias in ksh

Monasingh

Good catch! It should be forward tick and not reverse tick as I put in my initial post.

Writing aliases and functions saves time and make things go faster. I would rather type "psg http" than "ps -ef|grep http|grep -v grep" every time I have to look for http procs. Just my personal preference!
:-)
work is fun ! (my manager is standing behind me!!)
Rodney Hills
Honored Contributor
Solution

Re: alias in ksh

You use the function like an alias-

psg oracle

will display processes with name "oracle".

Give it a try, I think it will do what you want.

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: alias in ksh

If you are running csh now, why are you wanting to use "ksh"?

If you are trying to make alias definitions that are interchangable between the 2 shells, then I think you won't be able to do that.

If you are converting over to "ksh" and dropping "csh" as your primary shell, then you can convert all your aliases to functions, place them in ".profile" to set them at login.
It is pretty easy to convert the alias to a function. example-
alias psg='ps -ef | grep -i | grep -v grep'

becomes

function psg {ps -ef | grep -i $1 | grep -v }

You could build these functions by replacting the following-
"alias" with "function"
"='" with "{"
trailing "'" with "}".

If you have a lot, you could use sed or awk or perl to build up .profile.

Aliases under ksh only do "word" substitutions at the beginning of a command. Their is no equivalent .

HTH

-- Rod Hills
There be dragons...
Kellogg Unix Team
Trusted Contributor

Re: alias in ksh

Rodney,

Thanks. The function works the following way -

psg() {
ps -ef|grep -i $1|grep -v grep
}

I can include my other aliases this way.

Thanks again
...Manjeet
work is fun ! (my manager is standing behind me!!)
Bill Hassell
Honored Contributor

Re: alias in ksh

The -C option (and -H and -o) are all 'special' features that are enabled via the XPG4 option. The man page is a bit cryptic about how to set it, but all that is necessary is to set the environment variable UNIX95. In the above example:

UNIX95= ps -f -C sh

The UNIX95= simply defines the variable with a null value (UNIX95 can be anything including null) and in POSIX shells like ksh, the variable assignment can be placed in front of the command. Without UNIX95= then ps doesn't know what to do with -C

The primary advantage of -C is that ps searches the EXACT match of the program name (and not the pathname or username or anything else). Thus:

UNIX95= ps -f -C java

will not find any users with java in their name (like java or hotjava) nor will it find processes with a pathname such as /opt/java/bin/java3) because it looks just at the process name.

-H is pretty nifty when looking for a hierarchy of parents and children, and -o allows you to construct your own version of ps as in:

UNIX95= ps -e -o vsz,pid,args | sort -rn

which shows the resident set size of all prgrams, their PID and the command line, then sorted by VSZ (which is in Kbytes)


Bill Hassell, sysadmin