LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating an alias in ksh that uses grep and includes 'grep -v grep' (https://www.linuxquestions.org/questions/linux-newbie-8/creating-an-alias-in-ksh-that-uses-grep-and-includes-grep-v-grep-4175420483/)

doug248 08-05-2012 11:49 AM

Creating an alias in ksh that uses grep and includes 'grep -v grep'
 
I need to create an alias for non-admin programmers who would like to check to see if a process is running on the linux server (kshell). I would like it to auto-exclude the grep process.

alias I would like:
alias psgrep='ps ux | grep $1 | grep -v grep'

example command they would run:
'psgrep webconsole'

However, that errors out. I see the following output:
> psgrep webconsole
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
grep: webconsole: No such file or directory

Thanks

btmiller 08-05-2012 01:21 PM

Strangely enough, it works for me if I switch the order of the greps, e.g.:

Code:

alias psgrep="ps ux | grep -v grep | grep $1"
I'm guessing that this is due to some strange way that bash and ksh interpret arguments in aliases, but I can't for the life of me think up why. I guess it's been too long since I've thought about the internals of all this. My suspicion is if you defined this as a function instead of an alias in bash, it should work in the original order, but I haven't tested it.

doug248 08-05-2012 02:07 PM

Yes that works! Thank you very much.


All times are GMT -5. The time now is 01:32 AM.