Sep 052015
 

From http://www.smallo.ruhr.de/award.html , dome things to improve in your (and my) scripts ;).

Anything that looks like

something | grep '..*' | wc -l

can usually be rewritten like something along the lines of

something | grep -c . # Notice that . is better than '..*'

or even (if all we want to do is check whether something produced any non-empty output lines)

something | grep . >/dev/null && ...

and
ps -l | grep -v '[g]rep' | awk '{print $2}'

Remember that sed and awk are glorified variants of grep. So why use grep at all?

ps -l | awk '!/[a]wk/{print $2}'

and

Rememeber, nearly all cases where you have:

cat file | some_command and its args ...

you can rewrite it as:


And there is something on kill -9:
No no no. Don't use kill -9.

It doesn't give the process a chance to cleanly:

1) shut down socket connections

2) clean up temp files

3) inform its children that it is going away

4) reset its terminal characteristics

and so on and so on and so on.

Generally, send 15, and wait a second or two, and if that doesn't
work, send 2, and if that doesn't work, send 1. If that doesn't,
REMOVE THE BINARY because the program is badly behaved!

Don't use kill -9. Don't bring out the combine harvester just to tidy
up the flower pot.

 Posted by at 11:51

Sorry, the comment form is closed at this time.