1

I am trying to create an alias which will list all process sorted by process name.

So if I write myps processname, it should return all the process names sorted by process names, excluding the grep command result itself.

Both of these are working fine

alias myps='ps aux|grep $1'

or just

alias myps='ps aux|grep '

But I am not able to use it like below

alias myps='ps aux|grep $1|grep -v grep|sort -k12'

I understand that while creating alias, passed variable is added at the last of the command. Limitation is that I cannot change .env or .profile file and I cannot create new shell scripts on my unix box.

Can someone suggest any way to achieve it by alias or something?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Utsav
  • 7,914
  • 2
  • 17
  • 38

1 Answers1

1

An alias just expands to the string it represents, it doesn't really take any arguments. A function, on the other hand, seems to be exactly what you're looking for:

mureinik@computer /tmp $ myps() { ps aux|grep $1|grep -v grep|sort -k12; }
mureinik@computer /tmp $ myps bash
mureinik 10020  0.0  0.0 122552  6344 pts/2    Ss+  Mar13   0:00 bash
mureinik 11987  0.0  0.0 124068  6588 pts/3    Ss   09:17   0:00 bash
mureinik  6541  0.0  0.0 122716  6660 pts/0    Ss+  Mar13   0:01 bash
mureinik  7609  0.0  0.0 122592  6420 pts/1    Ss+  Mar13   0:00 bash
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thanks. Let me try this – Utsav Mar 15 '16 at 07:50
  • The `grep -v grep` can be avoided with something like `grep "[${1:0:1}]${1:1}"` which also fixes the [quoting](http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable) error. The substring facility is a Bash extension, though. – tripleee Mar 15 '16 at 07:57