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
bash scripting help
Hey,
May be this is what you were asking for....
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
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
Regards
Re: bash scripting help
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).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
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"
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
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.
-
- Havaldaar
- Posts: 105
- Joined: Thu Dec 25, 2003 2:31 pm
Thanks for the idea . i will further correct this code
soni wrote:Hey,
May be this is what you were asking for....
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.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
Regards