Ksh: Send a mail in case grep finds something


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ksh: Send a mail in case grep finds something
# 8  
Old 09-24-2014
your problem solved...
try
Code:
var=$(grep somestring somefile) && [[ ! -z "${var}" ]] &&  echo "${var}"|mailx -s "Found something" email@mycomp.com

Courtesy : Pilnet
This User Gave Thanks to Makarand Dodmis For This Post:
# 9  
Old 09-24-2014
Thanks, but the result from grep may have multiple lines, that will be clutched together when stored in one variable.
# 10  
Old 09-24-2014
If not too many matching lines, store them in a variable!
Code:
special=`grep somestring somefile`
if [ -n "$special" ]
then
  echo "$special" | mail ...
fi

---------- Post updated at 12:58 PM ---------- Previous update was at 12:35 PM ----------

Just seeing this is in principal the same as the previous answer.
No, the shell does not clutch anything together.
An echo $var does a reformatting, but not an echo "$var"
This User Gave Thanks to MadeInGermany For This Post:
# 11  
Old 09-25-2014
Outch, sorry Makarand, I missed that small difference the double quotes make around the variable.
Thanks (from Germany), also to "Made in Germany" for pointing that out.

Makarand's solution can even be abbreviated, as grep gives returncode 0 if it was successful (and 1 if not):

Code:
var=$(grep somestring somefile) &&  echo "$var"|mailx -s "Found something" email@mycomp.com

Does anybody know where the limit is for the size of a variable (in ksh 88)?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Client was not authenticated to send anonymous mail during MAIL FROM (in reply to MAIL FROM comm

I am having trouble getting mail to work on a red hat server. At first I was getting this message. Diagnostic-Code: X-Postfix; delivery temporarily suspended: connect to :25: Connection refused Then added the port to my firewall. Then I temporarily turned off selinux. I then copied this file... (1 Reply)
Discussion started by: cokedude
1 Replies

2. UNIX for Beginners Questions & Answers

Read a file and send mail based on grep

Hi All, I am having a job and I need to send email when the job is running. On any other case (success,fail) I don't needed to send email. I check with BMC they told they dont have that in the version I am using. So I created a dependent job and grepped for the status and sent email. My... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

3. UNIX for Beginners Questions & Answers

Grep the 'not running' jobs and will send the update in mail with its name(job)

my request is: i have to create a script , which will grep the 'not running' jobs and will send the update in mail with its name(job) Scenario: logged in to machine abc went to particular path: cd /a/b/c then ./script1.sh status (script.sh is a script,whose status gives info about 10 jobs... (1 Reply)
Discussion started by: learner987
1 Replies

4. Shell Programming and Scripting

I want my script to NOT to send an e-mail if it finds the same keyword more than twice.

My script triggers and e-mail if keywords supplied to it were found. Problem is if it find the same keyword continously (due to continous server errors), it triggers mails and fillup my mail box with same message (which is not required) I want my script to NOT to send an e-mail if it finds the... (13 Replies)
Discussion started by: Rajeshneemkar
13 Replies

5. Shell Programming and Scripting

ksh program that finds the lowest number in a .txt file

i am having a problem finding the lowest number after punching in a bunch of numbers in the .txt file but its probably the way i have the code set up. help please! (4 Replies)
Discussion started by: tinsteer
4 Replies

6. UNIX for Advanced & Expert Users

need to configure mail setting to send mail to outlook mail server

i have sun machines having solaris 9 & 10 OS . Now i need to send mail from the machines to my outlook account . I have the ip adress of OUTLOOK mail server. Now what are the setting i need to do in solaris machines so that i can use mailx or sendmail. actually i am trying to automate the high... (2 Replies)
Discussion started by: amitranjansahu
2 Replies

7. Shell Programming and Scripting

Calling SQL script from ksh job and send mail on some error.

Hi, I am trying to call sql script from ksh job with parameters.The parameters passed from ksh job will be used in SELECT query in sql file to SPOOL the data in extract file.My questions are: 1) How to call a sql script from ksh job with parameters? 2) How to use the parameter in sql file to... (1 Reply)
Discussion started by: anil029
1 Replies

8. UNIX for Dummies Questions & Answers

How to send html file in a mail not as an attachment but it should display in the mail in table for

Hi The below script working when we are sending the html as attachment can u please guide how to send thesmae data in table form direct in the mail and not in mail attachment . cat Employee.sql SET VERIFY OFF SET PAGESIZE 200 SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON - HEAD... (0 Replies)
Discussion started by: mani_isha
0 Replies

9. Shell Programming and Scripting

Send an e-mail using ksh

Hi, I have a small script that outputs to a text file. I need to e-mail the contents of the text file to a mail alias. However, I cannot seem to get the script to print the 'subject', my script just leaves it blank. Has anybody any ideas what is wrong? if then (echo "\nHere is the report... (3 Replies)
Discussion started by: asulli01
3 Replies

10. Shell Programming and Scripting

Appending to filename a string of text grep finds

I am wanting to automate a process that includes the step of appending to a filename a string of text that's contained inside the file. I.e. if filename A.fileA contains a string of text that reads 1234 after the phrase ABC, I want the shell script file to rename the file 1234_FileChecked_A.fileA.... (3 Replies)
Discussion started by: HLee1981
3 Replies
Login or Register to Ask a Question