It is currently Sat, 09 Dec 2023 05:25:23 GMT



 
Author Message
 grep sTerm * | grep -v grep ???
in .bashrc

alias r='ps ax | grep $1 | grep -v grep'

this works from the command line but doesn't work from the bashrc.
Apparently there's a syntax problem.

...and

. filename

where can I read about this commad that is found in bash and sh
scripts?  I searched the bash and one or two other man pgs to no
avail.  Thanks gurus.

--

Mike McNally            mmcnal...@prodigy.net



 Thu, 27 May 2004 07:31:46 GMT   
 grep sTerm * | grep -v grep ???
On Sat, 8 Dec 2001 mmcnal...@prodigy.net wrote:

An alias doesn't know about positional parameters.
If you type:
        r cron
it expands to:
        ps ax | grep $1 | grep -v grep cron
which is not what you want at all.

Use a function instead:
        r() {
             ps ax | grep "[${1%${1#?}}]${1#?}"
        }

The gobbledygook places square brackets around the first character of the
argument so that it won't match itself, and you don't need the second
grep.

A shorter way, but not POSIX compliant (it works in bash and ksh93) is:

        ps | grep "[${1:0:1}]${1:1}"

man bash:
       .  filename [arguments]
       source filename [arguments]
              Read  and execute commands from filename in
              the current shell  environment  and  return
              the  exit  status  of the last command exe-
              cuted from filename.  If filename does  not
              contain  a  slash,  file  names in PATH are
              used to find the directory containing file-
              name.   The  file searched for in PATH need
              not be executable.  When  bash  is  not  in
              posix   mode,   the  current  directory  is
              searched if no file is found in  PATH.   If
              the  sourcepath option to the shopt builtin
              command is turned  off,  the  PATH  is  not
              searched.   If  any arguments are supplied,
              they become the positional parameters  when
              filename  is executed.  Otherwise the posi-
              tional  parameters  are   unchanged.    The
              return  status  is  the  status of the last
              command exited within the script (0  if  no
              commands  are executed), and false if file-
              name is not found or cannot be read.

(Not all shells will allow arguments.)

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



 Thu, 27 May 2004 08:05:17 GMT   
 grep sTerm * | grep -v grep ???

Note that most systems have a command to do this:

Linux  : pidof (ps -p "$(pidof "$1")")
Solaris: pgrep or pfind I don't remember
HP-UX  : UNIX95= ps -C "$1"
FreeBSD: killall -vse "$1"
???

(to be continued).

--
Stphane



 Thu, 27 May 2004 17:32:32 GMT   
 
   [ 3 post ] 

Similar Threads

1. tail | grep | grep | grep

2. Bug in grep or man grep

3. PLEASE HELP A NEWBIE: grep doesn't grep

4. grep question New to grep

5. grep attempts to grep in non-ascii files

6. /bin/grep vs /usr/ucb/grep

7. lscfg | grep en and then grep lo

8. grep and grep -o line count differ

9. Using grep -v and grep -C together

10. grep [f]oo and grep foo differences


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software