coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem using dd within bash script


From: Bob Proulx
Subject: Re: Problem using dd within bash script
Date: Sun, 11 May 2014 17:48:24 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

Bernhard Voelker wrote:
> Sebastian Rückerl wrote:
> >while   ps ax | grep " $dd_pid "     # might also need  | grep -v grep  here

Bernhard's comment improves this greatly.

> The construct with "ps ax | ..." is rather fragile.
> 
> I'd use something like the following instead to get
> the intermediate I/O statistics:
> 
>   while kill -s USR1 "$dd_pid" ; do

That is much better.  But I wanted to comment upon the often seen ps
piped to grep piped to grep -v grep.  That is another one of those
constructs that has better ways but we still see people doing it.

Why do people grep -v grep?  To remove grep from the list.  Why is
grep in the listing?  Because they used ps in the simple list
everything mode that includes grep's command line too and the grep
pattern matched.  How to avoid it?  Don't list everything with ps! :-)

The ps command we all use is multi-styled.  It has a BSD style like
the BSD ps.  It has a System V style like the SysV ps.  And more.  So
at a first pass use the style that only prints the process name and
not the full command line.

  ps -e

That won't list the command line and therefore won't list the grep
pattern and therefore won't hit the grep too.  Much simpler.  And
standard too.

  ps -e | grep fooprocess

But wait, there's more...  Right in the ps man page it says:

       Print only the name of PID 42:
          ps -p 42 -o comm=

So if you know the process id already as above then instead of
grep'ing through all of the output it would be better to simply tell
ps to only list that process.  If there is no process by that number
then there will be no output.

If you want to list by name then the man page also shows:

       Print only the process IDs of syslogd:
          ps -C syslogd -o pid=

The return code is set appropriately in the above so there is not a
need to grep.  One ps command can do it all by itself.

And there is also the ability to specify user defined output.  It is
really quite versatile.

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]