Raspberry Pi automate certain tasks – script example

Now, if you have followed these :

fetchmail

ssmtp

Then you already have a working system for sending and receiving mail. Now, you can set the mda in the fetmailrc to a script which can do few things for you. The script below will get a page and mail it to you, if you have the subject as “get” and send “wake on LAN” to desired PC if you have subject as “wol”. Cool 🙂

Original idea from here.

Here is the script:

#!/bin/bash

expectedFrom=""
expectedFrom2=""
homePC="MAC Here"

mailHelp() {
	sendMail $1 "Help - Possible Commands" "Help"
}

sendMail() {
	echo "To: "$1 > $tmpMail
	echo "From: From address here" >> $tmpMail
	echo "Subject: "$2 >> $tmpMail
	echo "Content-Type: text/html" >> $tmpMail
	echo "" >> $tmpMail
	echo $3 >> $tmpMail
	cat $tempMail >> $tmpMail
	cat $tmpMail|/usr/sbin/ssmtp  $1
}
#here we start the actual processing

rightSender=0
sender=""
tmpFile=/tmp/mailtemp
tmpMail="/var/tmp/mailtxt.txt"
tempMail="/tmp/tosend.txt"
>$tempMail
>$tmpFile
>$tmpMail

#Write the mail to tmpFile
while read line
do
	echo $line >> $tmpFile
done

grep "From:" $tmpFile | grep $expectedFrom > /dev/null
if [ $? -eq 0 ]; then
	rightSender=1
	sender=$expectedFrom
fi

grep "From:" $tmpFile | grep $expectedFrom2 > /dev/null
if [ $? -eq 0 ]; then
	rightSender=1
	sender=$expectedFrom2
fi

if [ $rightSender -eq 1 ]; then
	task=`grep "Subject:" $tmpFile`
	task=${task:9}
	task=`echo $task | tr [:upper:] [:lower:]`

	echo $task | grep "help" > /dev/null
	if [ $? -eq 0 ]; then
		mailHelp $sender
		exit
	fi
fi
while read line
do
	if [[ $line =~ ^$|^Content*:*|^charset=*|^--=* ]]; then
		continue;
	fi
	echo "LINE :: $line"
	case $task in
		get)
			cmd="wget -o /dev/null -O - $line"
		;;
		wol)
			cmd="sudo etherwake $$line"
		;;
		*)
			cmd="$line"
		;;
	esac
	eval $cmd >> $tempMail
done < <( sed '1,/^$/ d' $tmpFile|sed '/^--$/,$ d')
sendMail $sender "Output result of command $task" "Happy Hacking"

rm $tmpFile
rm $tmpMail
rm $tempMail
Enhanced by Zemanta

some interesting alias

For this time, I will just give you a link to to bashrc file.

http://hayne.net/MacDev/Bash/aliases.bash

Head over there and see some very interesting aliase’s.

Enhanced by Zemanta

The power of find command in Linux – advanced.

Generally whoever uses Linux, would know about the find command. Find the man page here.

There are also lots of blogs, tutorials and other articles on find command on the web, so why write another one. Because it\’s worth every word spent on it 🙂
find is a very powerful command, let\’s see how (options for find command from man page and usage):

depth — Process each directory\’s contents before the directory itself.
maxdepth — Descend at most levels (a non-negative integer) levels of directories below the command line arguments.
xdev — Don\’t descend directories on other filesystems.
executable — Matches files which are executable and directories which are searchable (in a file name resolution sense).
This takes into account access control lists and other permissions artefacts which the -perm test ignores.
iname — Like -name, but the match is case insensitive.
nogroup — No group corresponds to file\’s numeric group ID.
nouser — No user corresponds to file\’s numeric user ID.
fls file — True; like -ls but write to file like -fprint.
ok command — Like -exec but ask the user first (on the standard input);
print0 — True; print the full file name on the standard output, followed by a null character
(instead of the newline character that -print uses).
printf format — True; print format on the standard output, interpreting \’\’ escapes and \’%\’ directives.

There are lot of other options but these are worth mention not used on regular basis. The printf option requires
special mention as the number of options that it supports is quite large and can reallyl help you in the formatting
the data that you get, as well as help you get the data that you want.

Example:
1) Get names of files bigger than 5M

find . +5M -printf \”Name : %20f Size : %s\\n\”

2) delete files (filenames contain spaces or quotes and so on)

find . -name \”name*\” -print0 |xargs -0 rm -f

Updated 28th May: Realinged for issues with post going into the sidebar 🙁