[SlugBug] Using cut in bash scripts

Craig Andrews craig at simplyspiffing.com
Thu Apr 6 19:31:06 BST 2006


On Thu, Apr 06, 2006 at 07:09:39PM +0100, Bill Best wrote:
> just quickly, i would like to extract the process number of the icecast 
> service running here and then give it a variable name:
> 
> >[root at machine ~] ps aux | grep icecast
> >iceadmin  9862  0.0  3.4 339200 17796 pts/126 S   16:04   0:00 icecast -c 
> >/etc/icecast.xml
> >root     10061  0.0  0.1  4444  592 pts/126  R    16:13   0:00 grep icecast
> 
> this is very wrong but might explain what i want the script to do:
> 
> ps aux | grep icecast | cut -f2 > /tmp/icecastproc.$$

Hi Bill,

You're probably best not using cut here, because it is a strange layout.
Also, you can drop the -u option to ps. That way, the process number
will be the first thing on the line.

The easiest thing might be to use awk, which has a very handy way of
splitting out chunks of a line like this:

    ps ax | grep icecast | grep -v grep | awk '{ print $1 }'

Note that I also removed the 'grep' line to prevent false positives.

HTH

-- 
Craig Andrews <craig at simplyspiffing.com>



More information about the SlugBug mailing list