|
LCOD - 11.17.09 - Simple page/alert email script |
|
Written by Jon Zobrist
|
|
Tuesday, 17 November 2009 |
Here's a quick page script for *nix boxes with mail.
I grabbed the mail / file redirect from
http://theos.in/shell-scripting/send-mail-bash-script/
#!/bin/bash
#Author :
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
#Description : Quick alert/page script, edit EMAIL="" to include a comma separated list of emails to send to
#Usage : page.sh "Short or long message"
if [ "${1}" ]
then
SUBJECT="[ALERT] ${1}"
EMAIL="
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
,
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
"
EMAILMESSAGE="/tmp/`date +%s`-message"
echo "ALERT: at `date`" > ${EMAILMESSAGE}
echo "${1}" >> ${EMAILMESSAGE}
echo "From `hostname`" >> ${EMAILMESSAGE}
mail -s "${SUBJECT}" "${EMAIL}" < ${EMAILMESSAGE}
/bin/rm ${EMAILMESSAGE}
else
echo "Usage: ${0} MESSAGE"
exit 1
fi
|
|
Last Updated ( Tuesday, 17 November 2009 )
|
|
|
LCOD 10.9.09 - Script to retry command |
|
Written by Jon Zobrist
|
|
Friday, 09 October 2009 |
I use this almost exclusively with trying to ssh to a machine that's rebooting, but it's useful to have, and, I think, should be included as a default command in any Linux distro.
Script : retry
Usage : retry command [args]
Code :
#!/bin/bash
while true
do
echo "trying $@ at `date`"
$@
sleep 1
done
Enjoy.
|
|
Last Updated ( Friday, 09 October 2009 )
|
|
|
LCOD - 6.18.09 Useful command line grepping (grep!) |
|
Written by Jon Zobrist
|
|
Saturday, 18 July 2009 |
There are a few regular expressions I find myself using all the time via grep on the command line. Here are a few, along with a few frequently used commands I can't live without.
Find the non comment parts of a file.
(Useful if you want to see or compare the active parts of a config file.)
Most Unix config files use # as a comment tag, indicating everything after the # is a comment.
grep '^[^#]' filename.conf
|
To break this down, first we have grep , which is a command line program to find lines that match a regular expression pattern, and display them. The single quotes indicate that we're passing the entire section enclosed in single quotes as the first argument, which doesn't matter as much in this example, but it's good to practice, so when you have a space or some other character, you won't be confused when your shell interprits it. The regular expression used is
^[^#]
Which is slightly trick, as far as regular expressions go, for only one reason; the character ^ is used twice, with different meanings.
Normally the ^ is regular expression for the start of a line, and we use it here as this, the first time. The second time, inside the brackets, it means NOT.
Brackets in regular expressions are like a giant OR statement, with every non-escape character (like the forward slash / ) as a possible match. This can hang people up when they expect [bob] to match the full word bob, instead of just the first b.
Then we have the #, which just matches our comment character, #.
Then the closing bracket, since we're not doing any other options.
The net result of this is to match any line that has a # right after the start of the line.
To do the same thing on a config file that uses a ; as a comment indicator:
grep '^[;]' filename.conf
|
OR, to be even trickier, you could just add the # and ; in the same set of brackets, like
grep '^[;#] fliename.conf
|
Note the order of items inside the bracket doesn't really matter in this case.
Next, often times I have a file with a bunch of empty lines in it, and I want to quickly get rid of them all. To do this I use a combination of a simple grep and a output redirect.
The . character is a regular expression match for any character, but it won't match empty lines (the regular expression ^$ does though).
To get this into the file, first redirect it to a new file, then move the file back.
grep . filename > newfile
mv newfile filename
or, if you want to sort, just run
sort newfile > filename
perhaps you now want to just get the unique lines
uniq newfile > filename
|
Of course, I should mention the very obvious ones.
Grepping for words in files.
Note that grep is case sensitive, but you can turn that off with the -i flag.
There is an infinite amount of uses for grep, but the foundation for grep/sed/awk/perl use for patterns is regular expressions. More than any single book I've read about techonlogy, the O'Reilly book "Mastering Regular Expressions", by Jeffrey
Friedl
has helped me the most in my IT career. I highly suggest you buy and read it.
|
|
Last Updated ( Sunday, 06 September 2009 )
|
|
|
|
<< Start < Prev 1 2 3 Next > End >>
|
| Results 1 - 4 of 10 |