1Sep/090
Using getops with Bash
Getopts is an easy tool to process and check your command line arguments in a bash script. Far easier than trying to parse out the options yourself!
Example script:
#!/bin/bash
# Example script using getops
usage () {
cat << EOF
Example Script
usage: $0 options
-h Display this message
-v Patch Version
-s Remote server to patch
EOF
}
while getopts "hv:s:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
v)
VERSION=$OPTARG
;;
s)
HOST=$OPTARG
;;
esac
done
if [[ ! -z $VERSION ]] && [[ ! -z $HOST ]]
then
echo "You selected $VERSION to be applied to $HOST!"
else
echo "You didn't tell me what to do."
usage
exit 1
fi
exit
21Aug/090
Grep and Multiline Regular Expressions
Nothing new here: needed to go through some old log files to find something. Unfortunately, I needed to match across 2 lines specifically. I wrote up my regex, and tested it in Reggy and all was good. I tried it out in the real world but didn't get any matches. GNU Grep and Egrep don't support \n! It only searches one line at a time. Enter: pcregrep -M.
(The other catch is that the files are compressed. bzcat and zcat solve this problem.)
The entry I was looking for:
I 23:21:37:465 aaaaaaa ZzzHandler: Subscription expired for x. D 23:21:37:466 aaaaaaa ZzzHandler: Proceeding to deactivate account
So I came up with this obfuscated 'for' loop:
for i in $(find /mnt/xxxxxxx -name xxxxxxx*bz2 -type f -print)
do
echo $i; bzcat $i | \
pcregrep -M ".*ZzzHandler: Subscription expired.*\n.*Proceeding to deactivate.*"
done | tee -a /tmp/xxxxxx-archived.log