Sendmail in a shell script

May 28, 2008

Someone asked me to write a script that will send mails to a particular email id. I had not used any such script before. So I had to search quite a lot. Here is the simple shell script that will send mail to the specified email ids. Currently it does not support attachments.

#!/usr/bin/ksh
#———————————————————————-
# Name: SendMail
# Purpose: To send mails using the sendmail command
# Usage: SendMail
# Owner: Ketan Joshi
# Setting: Just change the variables at the start of the script to
#          appropriate values. Create a message by modifying the string BODY
#          You can even have html tags in the body.
# Limitation: Currently, this does not support attachments.
#——————————————————————–
#Temporary file for containing the mail message
tmp=/tmp/mail-body-`date +%F`;
touch $tmp && chmod 600 $tmp;
#Set up the various headers for sendmail to use
TO=’ketan.joshi@oracle.com,saurabh.abhishek@oracle.com’;
CC=”;
FROM=’ketan.joshi@yahoo.com’;
SUBJECT=’Test Mail’;
MIMEVersion=’1.0′;
CONTENTType=”text/html; charset=us-ascii”;
#Here write the content of your mail.
BODY=”
<b>Hello from ketan.</b>
This is test mail.
“;

echo Sending the mail.
echo -e “To: $TO” > $tmp;
echo -e “Cc: $CC” >> $tmp;
echo -e “From: $FROM” >> $tmp;
echo -e “Content-Type: $CONTENTType”>>$tmp;
echo -e “MIME-Version: $MIMEVersion”>>$tmp;
echo -e “Subject: $SUBJECT”>>$tmp;
echo -e “Body: $BODY”>>$tmp;

/usr/sbin/sendmail -t < $tmp;

rm -rf $tmp;


VI commands

May 24, 2008

VI COMMANDS

I got this list somewhere on the net. I have not reviewed it completely, but at the first glance, this looks correct. Enjoy…

CURSOR & LINE POSITIONING
h  Move left one character.
i  Insert text move into insert mode before the current character.
j  Move down one line.
k  Move up one line.
l  Move right one character.
0  Move to beginning of line.
$  Move to end of line.
Backspace Move left one character.
Enter Move to beginning of next line.
(  Move backward one sentence.
)  Move forward one sentence.
+  Move down to beginning of next line.
–  Move upto the beginning of previous line.
G  Move to last line in file.
nG Move to line no. n in file.

WORD POSITIONING
B  Back up one space-delimited word.
b  Back up one word.
W  Move forward one space-delimited word.
w  Move forward one word.
e  Move to the end of the current word.

WINDOW POSITIONING
H  Move to first line on screen i.e. “Home”.
M  Move to middle line on screen.
L  Move to last line on screen.
^f Move forward one screen of text.
^b Back up one screen of text.
^d Move down half a page.
^u Move up half a page.

TEXT EDITING
a  Append text move into insert mode after the current character.
A  Append text at end of current line.
i  Inserts text at cursor.
I  Inserts text at beginning of current line.
Esc Leave insert mode, return to command mode.
R  Overwrite text from cursor position.
r  Replace the current character with the next pressed.
O  Open new line for insert above the current line.
o  Open new line for insert below the current line.
U  Undo replace current line if changed.
u  Undo the last change made to the file.
:e file   Edit a specified file without leaving vi.
C  Change text through the end of line.
c  Change text in the specified range cw changes the following word,
whereas c} changes the next paragraph.
{  Move backward one paragraph.
}  Move forward one paragraph.

DELETION
x  Delete a single character at current position.
X  Delete to left of cursor.
dw Delete a word from cursor to the next space/punctuation.
dd Delete a line.
nx,ndw,ndd   Delete n chars,n words,n lines.
d0  Delete line from cursor to beginning of line.
d$  Delete line from cursor to end of line.

MISCELLANEOUS
^g Show current line number and other information about the file.
^v Prevent vi from interpreting the next character.
^l Clears and redraws the current window.
:! Invoke specified UNIX command.
J  Joins the line below current line with current line.
~  Changes current char to Upper case from lower case and vice versa.
:sh Return to Shell. Type exit to get back to vi.
.  Repeats action performed by last command
!!  Replace current line with output of UNIX command.
!}  Replace current paragraph with the results of piping it through
the specified UNIX program or programs.

QUITTING
ZZ  Write buffer to file and quit.
:w  Write the edit buffer to the system.
:q  Quit vi if changes have been written.
:q! Quit vi throwing away any changes made.
:w file  Writes buffer to specified file.
: w! file  Overwrites specified file with contents of buffer.

SEARCHING
/pattern  Search forward for the next line using a specified pattern.
?pattern  Search backward for the next line using a specified pattern.
n  Repeats the last search command.
N  Repeats search command in opposite direction.

BLOCK COMMANDS
:r file  Read the contents of a specified file, including it in
the current edit buffer.
:m,n d  Deletes lines from m to n.
:m,n mo p Moves lines m to n after line p.
:m,n co p Copies lines m to n after line p.
:m,n w file  Writes lines m to n to specified file.
:m,n w>>file Appends lines m to n to specified file.
:r !command  Execute shell command and output of command is read
at current position.

FIND AND REPLACE
:s/str1/str2  Replaces first occurence of str1 with str2 in current line.
:s/str1/str2/g  Replaces all occurences of str1 with str2 in current line.
:m,n s/str1/str2/g range of command from m to n.
:1,$ s/str1/str2/g range of command from first to last line.
:.,$ s/str1/str2/g range of command from current line to last line.
:1,. s/str1/str2/g range of command from first line to last line.

DELETE AND PASTE
“nP  To Paste nth latest deletion after current line.n range is 1-9.
“xdd,xndd,xdw,xndw Deletes 1 or n lines, words and stores in buffer named x.
    (x can be any alphabet)
“xp  Pastes from buffer named x at current cursor position.

COPY(Yank) AND PASTE
yw  Yanks word from cursor position.
yy  Yanks line from cursor position.
y$  Yanks line from cursor position to end of line.
Y0  Yanks line from cursor position to beginning of line.
nP  Pastes nth latest yanked buffer. n range is 1-9.
“xyy,”xnyy  Yanks 1/n lines to buffer named x.
“xyw,”xnyw  Yanks 1/n words to buffer named x.
“xp  Pastes buffer x at current position.

ABBREVIATION
:abbr a bcd   Define abbreviation a for phrase bcd.
:abbr   Show current abbreviations, if any.
:una a Unabbreviates the abbreviation a.
:map a bcd    Map key a to the vi commands bcd.
:map   Show current key mappings, if any.

MULTIPLE FILES
vi fn1,fn2,fn3  Loads fn1,fn2,fn3 into vi for editing.
:n  Move to the next file in the file list.
:n! Move to next file in list without saving current file.
:rew  Move to first file in list.
:rew! Move to first file in list without saving current file.
:args Displays file list in buffer.
:f  Displays name of current file.

SET COMMANDS
:set nonu  Turn off line numbering.(Default)
:set nu    Turn on line numbering.
:set eb    Turn Speaker on.(Default)
:set noeb  Turn speaker Off.
:set ai    Turn auto indent on.
:set noai  Turn auto indent off.(Default)
:set ic    Ignore case in Pattern search.
:set noic  Do not Ignore case in Pattern search.(Default)
:set mesg  Permit reciept of message from other terminals.
:set nomesg  Do not recieve messages. (Default)
:set showmode  Display current working mode.
:set noshowmode  Do not display current working mode.(Default)
:set aw  Auto write buffer to disk before switching to next file.
:set noaw  Do not write buffer to disk before switching to next file.(Default)


Taking backup of your files

May 22, 2008

Here is a small script that will automatically create a backup directory and take a backup of your files.

The script needs many improvements but, in its simplest form, I found it useful. I’ll improve it as and when I get some time.

#! /usr/bin/ksh
echo “Backing up $1”;
file=$1;
vDate=`date +%Y%m%d_%H%M%S%p`;
# User can mention relative or full path of the filename.
# in this case, extract the basename.
#fileName=split(‘/’,$1)
newFile=${file%\.*}_$vDate.${file##*.};
if [[ $1 = “” ]]
then
echo “Usage: backup <filename>”
exit;
fi
if ! [ -d ‘bkp’ ]
then
echo “bkp directory is not present. Creating the same.”
mkdir bkp;
fi
echo “copying $1 to bkp/${newFile}”;
cp $1 ./bkp/${newFile};
chmod -wx ./bkp/${newFile};

How to use this?

I have saved this script as “backup” and given it execute permission.

To take a backup of a file, run the following command at command prompt:

$ backup <filename>

This will first check if a directory “bkp” is present in the current directory. If not, it will create it and copy the file to it with name changed to reflect the date and time of copying.

Currently, it handles only one file at a time, I need to modify this to handle multiple files. But I guess I will do that in Perl instead of shell as it looks simpler.


20 May 2008

May 20, 2008

Dear Diary,

I got my results for second sem today. Actually, the results were declared yesterday. But I got to see them only today. It was a very pleasant surprise to see that I had cleared all the subjects. Even more surprising was that I had managed to score more marks than last time. I managed to raise my CGPA to 6.33 which I think is great.


Harry Potter and Me

May 12, 2008

DISCLAIMER: This is neither a book review nor a promotional attempt.

What is your favourite book?

If this question were asked before I came to Hyderabad, I would have said “The Da Vinci Code” or “The Godfather” or something like that. But something happend when I came to Hyderabad that changed my opinion.

When I first arrived in Hyderabad, I hardly knew anyone here. All my friends and such were either in Pune or Mumbai (I prefer to call Mumbai rather than Bombay like any good Marathi person 🙂 ). Although I got to know people from my project at Oracle, I could hardly count anyone as friend at that time. My daily schedule was pretty boring. Getting up @ 9, reaching to office @ 10:30, working (?), leaving for home @ 6:30PM and sleeping @ 11PM. I was very frustrated.

Then, I decided to read Harry Potter.

Now, I had already heard of this book and even watched one of the movies. And I had always believed that this was a book for kids. I knew friends who loved this book and I always made fun of them (you see, at that time I counted myself as an adult).

At first I purchased only the first part. After all, my decision to read Harry Potter was born out of pure frustration. I thought that, by the time I finished this, I might as well have found new friends and hoped against all odds that I won’t need to read the other parts. How very wrong I was!!!

Well, as it turned out, the more I read the book, the more I wanted to stay home and keep reading it. It was addictive, though in a good way. In no time at all, I had finished the first part and I found myself in the Hyderabad Central looking for the second part. I read all the 6 parts within 3 months. The seventh part was yet to be released then. I think, that was the fastest I had ever read anything, considering that I had to be in office for 9 hours every day. I was soon craving for the last part to be released. I could not read anything else due to the anticipation of the seventh part and hence read the first 6 parts again.

This was the first and only book that I had managed to read more than once. And I am including my textbooks in this also. This was a real surprise even for me. I started surfing the net looking for articles related to Harry Potter, started searching for clues of what might be coming in the last part. I even joined one of the fan sites. In short, I was taken by the PotterMania. When the last part was released, I was 2 weeks away from my end-sem exam. And still, I could not help but finish reading it within first 72 hours.

Now, its 18 months since that moment when I had bought the first part, and I have read the whole series more than 3 times in paper and listened to the audio version more than 4 times (I somehow don’t like the movies this much). I dont know why I liked it this much. But I think, it was the fact that I was feeling lonely and I found a friend in these books. I have always liked reading. But this was something special. The characters in the story were lively and friendly. These characters were living the life I had always dreamed. I was living my dream through these books. There were times when I could even picture the characters moving and hear them talking while I read the story. I found it easy to go through my dull work life when I was listening to or reading these books.

I don’t know how many more times I will read these books or listen to its audio versions. But I am certain of picking them up whenever I feel low or frustrated. It always lifts my mood.

Now my friends make fun of me because of my Potter Mania (Just like I used to do with my friends 2 years earlier). But I tell them that this is my passion. And atleast, I have one.

So if anyone asks me now what my favorite book is, my definite answer would be Harry Potter. Other books will come and go, I might read them, I might even like them, but I doubt if there would be another Harry Potter.