bash scripting help

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
kernel-trap
Havaldaar
Posts: 105
Joined: Thu Dec 25, 2003 2:31 pm

bash scripting help

Post by kernel-trap »

bash scripting help
Basically i want make script when i mail it with specfic command
it will execute command on system and mail back command results

i have problem after excuting command how i can remove command from
my mail box file . so the when i run the script again it wont execute it again



here is code

#!/bin/bash

a=$(grep "command*" /var/mail/kerneltrap)

case "$a" in
command1)
uptime > myuptime
mail kerneltrap@mydomain.com < myuptime
;;
command2)
date > mydate
mail kerneltrap@mydomain.com < mydate
;;
esac
soni
Naik
Posts: 70
Joined: Sat Oct 04, 2003 1:44 pm
Location: Karachi
Contact:

Post by soni »

Hey,

May be this is what you were asking for....

Code: Select all


#!/bin/bash
#In the mail directory there is a file, called "kerneltrap".
#In this mail-file the "command*" is right at the beginning of the new line. 
#Donot put the script, where the mail-file(kerneltrap) is.

kerneltrap='/var/mail/kerneltrap'
textfile='/var/mail/textfile'
a=$(grep "command*" $kerneltrap)

case "$a" in 
command1)
sed s/command1// $kerneltrap > $textfile
cat $textfile > $kerneltrap
rm $textfile
uptime > myuptime
mail kerneltrap@mydomain.com < myuptime
;;
command2)
sed s/command2// $kerneltrap > $textfile
cat $textfile > $kerneltrap
rm $textfile
date > mydate
mail kerneltrap@mydomain.com < mydate
;;
*)
echo ':('
;;
esac

The above script is not perfect, read the man for the grep, awk and sed. You'll find various other options which'll help you to write a better solution than this.

Regards
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Re: bash scripting help

Post by lambda »

kernel-trap wrote:bash scripting help
Basically i want make script when i mail it with specfic command
it will execute command on system and mail back command results
use procmail for something like this. procmail is better because it handles mailbox locking issues, and is designed for scanning mail messages (it understands the mail header format, for example).

install procmail and create a .forward file in your home directory. put this in it:

Code: Select all

"|exec /usr/local/bin/procmail -f- || exit 75"
make sure it points to the right procmail binary. next, create a file in your home directory called .procmailrc:

Code: Select all

DEFAULT=/var/mail/you
LOGFILE=$HOME/procmail.log
VERBOSE=on
FROM=`formail -rt -xTo:`

:0 B :
* !^From +YOUR_USERNAME
* !^FROM_DAEMON
* uptime
| uptime | mail -s uptime $FROM

:0 B :

* !^From +YOUR_USERNAME
* !^FROM_DAEMON
* date
| uptime | mail -s uptime $FROM

(don't replace YOUR_USERNAME with your username.)

this will scan your incoming mail for messages with "uptime" and "date" in the body, and return messages with the results of running those commands. if neither of the two rules are matched, the mail ends up in the DEFAULT location -- your inbox. note that this recipe isn't tested -- you can potentially lose mail here. test it on a separate account.

there are better ways of doing this, by the way. see the man pages for procmail, procmailex, procmailrc, formail -- especially the "retrieve" example in procmailex's page. the retrieve example is almost exactly what you need.
kernel-trap
Havaldaar
Posts: 105
Joined: Thu Dec 25, 2003 2:31 pm

Post by kernel-trap »

Thanks for the idea . i will further correct this code :)
soni wrote:Hey,

May be this is what you were asking for....

Code: Select all


#!/bin/bash
#In the mail directory there is a file, called "kerneltrap".
#In this mail-file the "command*" is right at the beginning of the new line. 
#Donot put the script, where the mail-file(kerneltrap) is.

kerneltrap='/var/mail/kerneltrap'
textfile='/var/mail/textfile'
a=$(grep "command*" $kerneltrap)

case "$a" in 
command1)
sed s/command1// $kerneltrap > $textfile
cat $textfile > $kerneltrap
rm $textfile
uptime > myuptime
mail kerneltrap@mydomain.com < myuptime
;;
command2)
sed s/command2// $kerneltrap > $textfile
cat $textfile > $kerneltrap
rm $textfile
date > mydate
mail kerneltrap@mydomain.com < mydate
;;
*)
echo ':('
;;
esac

The above script is not perfect, read the man for the grep, awk and sed. You'll find various other options which'll help you to write a better solution than this.

Regards
Post Reply