Shell Script: GetCluster – a shell script I happen to have written today; uses a few features of GNU Grep, whilst being tolerant of non-GNU greps (at the cost of some functionality).
This simple shell script is an example of the kind of thing that can easily be done with a few simple commands. As it is intended for the Solaris platform, which does not necessarily include GNU’s grep, some additional complexity is required to see what the platform’s grep utility is capable of.
This is achieved by:
1) Set the PATH to pick up the GNU tools first: PATH=/usr/sfw/bin:$PATH
2) Find grep (ggrep if possible; the existence of ggrep suggests that grep itself is not GNU’s grep):
type ggrep >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
GREP=`which ggrep`
else
GREP=`which grep`
fi
It then finds a file, which could be in one of many places:
CLUSTERTOC=`ls clustertoc .clustertoc /cdrom/cdrom0/Solaris*/Product/.clustertoc /cdrom/Solaris*/Product/.clustertoc /mnt/Solaris*/Product/.clustertoc 2>/dev/null|head -1`
What this does, is identify the first succesful match (the rest go to /dev/null). If none is found, the CLUSTERTOC will not be a valid file (note: this gets us the added bonus that it will complain if clustertoc is a device driver, pipe, etc).
It then looks for “SUNW_CSRMEMBER” in the clustertoc, using “grep -n
” to get the line number, then passing that linenumber on to head
so as to limit the output to only contain those lines. Any lines below that point would refer to other clusters, and be irrelevant. We want to get the last of the relevant part of the file, so that the tail
command below will give us what we need.
GNU Grep has a “-A n” and “-B n” facility to say “include n lines After the matching line” or “include n lines Before the matching line”. We use “-A” here to get the following lines which describe the pacakge in more detail:
head -${linenum} $CLUSTERTOC |$GREP -A4 "^CLUSTER="|cut -d"=" -f2-|tail -4
See the readme file for specific output – the Solaris (non-GNU) version has notably less detail than the GNU version.
Hopefully the script (http://steve-parker.org/code/sh/getcluster/getcluster.sh) is in other ways sufficiently self-explanatory.
The input file (http://steve-parker.org/code/sh/getcluster/clustertoc is relatively simple, and should explain any other queries.
Still – this simple script may provoke a few questions; please do play with it and ask away, if anything is unclear.
Don’t forget that
grep
has three (at least GNU Grep, I should install different Unices to VirtualBox to test) differentregex
matching options – if I remember correctly, basic one (which is not much of use), extended (which is OK) and Perl regex, they make matching, especially in complicated shell scripts (or even applications written in shell script) that are very useful in some situations. Without that you might have to call some other heavier and more complex program (which is not as guaranteed to exist asgrep
).Also people reading this should note that, as man page of
egrep
states, it is not recommend to useegrep
instead ofgrep
–grep
can do all regex greppings too anyway.