The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 09:25 AM
Why generate "ash and bash" different output for same bash script? s. murat Shell Programming and Scripting 0 05-26-2008 04:19 AM
alias klannon Filesystems, Disks and Memory 6 04-27-2006 05:36 AM
alias help shafique HP-UX 4 11-19-2005 11:19 PM
Query regarding alias and setting bash as a default script VENC22 UNIX for Dummies Questions & Answers 1 07-13-2005 06:18 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-11-2008
Registered User
 

Join Date: Jun 2008
Posts: 35
Stumble this Post!
bash alias help

I try the following alias:
alias psx='ps auxw | grep $1 | grep -v grep'
but it fails with "grep: yyy: No such file or directory" (for 'psx yyy' command).

the following is ok:
alias psx='ps auxw | grep $1'

why this happens?
how to overcome this?

thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-12-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
An alias cannot have an argument like $1. You want to use a function instead.

Code:
psx () {
  ps auxw | grep "$1" | grep -v grep
}
Your second alias works by coincidence when $1 is empty in the current shell (as it will often be in an interactive bash); it expands to ps auxw | grep (empty string) yyy -- and ironically, if you had double-quoted $1 properly, it would also not have worked.

Your first alias expands to ps auxw | grep (empty string) | grep -v grep yyy

Search these forums for ways to avoid the grep -v grep elegantly; solutions have been posted a number of times.
Reply With Quote
  #3 (permalink)  
Old 06-12-2008
Registered User
 

Join Date: Jun 2008
Location: Scotland
Posts: 102
Stumble this Post!
You might it helpful to investigate the pgrep command if it is available on your system (I think it is on Solaris and Linux).
Reply With Quote
  #4 (permalink)  
Old 06-12-2008
Registered User
 

Join Date: Jun 2008
Posts: 35
Stumble this Post!
thanks!

Apparently many make this mistake, I found more than once $1's for aliases in .bashrc files...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0