Zip the files if count is more than 0 and send a mail


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Zip the files if count is more than 0 and send a mail
# 1  
Old 08-03-2012
Zip the files if count is more than 0 and send a mail

All,

I am new to shell scripting and trying to get the count of files that starts with error and with extension .out, if the count is greater than 0 and zip the file and send an email with the content of error.out file, here is my script
Code:
cd /temp
testcount =$('find . -name '*.out' -print | wc -l')
echo $testcount
if [[ $testcount -ne 0 ]]
then
zip error_log.zip *.out
cat error_*.out | mailx -s "for testing" username@doamin.com
exit 1
else
exit 0
fi

Moderator's Comments:
Mod Comment code tags, please

when I tried to execute above script I am getting below error

./test.ksh: line 2: find . -name *.out -print | wc -l: command not found
./test.ksh: line 2: testcount: command not found


Can someone please tell me what is wrong with this script?
# 2  
Old 08-03-2012
You have an extra single-quote in front of find, remove it.
# 3  
Old 08-03-2012
Code:
cd /temp
testcount =$('find . -name '*.out' -exec wc -l {} \; | awk '{sum +=$1 } END {print sum} )
echo $testcount
if [[ $testcount -ne 0 ]]
then
zip error_log.zip *.out
cat error_*.out | mailx -s "for testing" username@doamin.com
exit 1
else
exit 0
fi

# 4  
Old 08-03-2012
Jim, you're counting the lengths of the files when I think the OP just wants the number of them.

I think you can get away with not using find, too:

Code:
set -- error*.out

if [ -f "$1" ]
then
        echo "$# files"
        stuff to zip and email
else
        echo "No files"
fi

Though set -- overwrites your $1 $2 ... parameters.
# 5  
Old 08-03-2012
Bug

Corona,
I am not counting the length of file , I am counting the number of files.

Jim,

I tried your code..but getting same error.

can you give me the solution for this, it might resolve my need

If I find any file that start with error and have an extesion .out...then send an email(which is working fine without any conditions) and exit 1(stopped abruptly) else exit 0 (exit normally)

your help is much appreciated...Thank You

---------- Post updated at 03:42 PM ---------- Previous update was at 03:05 PM ----------

Corona,

your code worked...Thank you so much for the help.Smilie
This User Gave Thanks to luckydoll For This Post:
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. Shell Programming and Scripting

Remove files send through mail

I wan't to remove last 5 and 10 days files from two directory and send the remove files list in mail Output send through mail find /kalia/cd/ -type f -mtime +5 exec rm {} \; find /kalia/uk/ -type f -mtome +10 exec rm {} \; Output send through mail File has been deleted ... (1 Reply)
Discussion started by: Kalia
1 Replies

3. Shell Programming and Scripting

Need Script to ZIP/SAVE & then DELETE Log file & send a mail conformation for any error

ENVIROMENT Linux: RHEL 6.4 Log Path: /usr/iplanet/servers/https-company/logs Log Format: user.log.03-15-2015 I have log4j log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I need a script that will run daily that... (1 Reply)
Discussion started by: admin_job_admin
1 Replies

4. Shell Programming and Scripting

Script for count files and send mails

Hi. I'm new on this forum and I need if possible someone to help me with one script. The script should act like this: - should be run by crontab and have next parameters: script_name $par1 $par2 $par3 $par4 where script will search in dir $par1 for files with mask $par2 and if number of... (2 Replies)
Discussion started by: atrailm
2 Replies

5. Shell Programming and Scripting

want to send 4 files output in one mail

Hi All , i have a wrapper HTML script -FS_CHECK_HTML_SCIPT_WT_Statusbar.sh which read a TXT file (script_KB.txt) and sends output to a mail in tabular format. TXT file is having 6 columns and so wrapper script create a table with 6 columns and supplies those 6 columns data from TXT file .... (2 Replies)
Discussion started by: deepakiniimt
2 Replies

6. Shell Programming and Scripting

How to send mail with multiple files attached?

Hi, I would like to send a mail with multiples files attached. Until now i was doing a "find" and then a "tar" of the text files. As I need to be able to read them on a mobile device such as Blackberry for instance, i would like to send them in one single mail, but without taring them. is... (2 Replies)
Discussion started by: Pierrito
2 Replies

7. AIX

Send mail with attachment having csv files

Hi, Could anyone please help me to send multiple files of .csv format in one mail. Thanks Aj (2 Replies)
Discussion started by: atinjain05
2 Replies

8. UNIX for Advanced & Expert Users

send all mail to files instead of relaying out

I had a wierd request from one of our guys. We are running on RedHat EL5. He asked that all mail originating from 3 servers he owns be sent to files instead of being sent out. My first thought is to set "Smart" relay host in sendmail.cf on each of the 3 servers so all messages are sent to a 4th... (0 Replies)
Discussion started by: mglenney
0 Replies

9. UNIX for Dummies Questions & Answers

Script to get Row Count of ZIP Files

Hi, I have some data files in a zipped format.(eg: aa.gz).I would like to know the number or rows in each zip file(May be populate the file name and line numbers into a text file).I know the commands wc -l and gunzip,.But how I will create a shell script for this to extract each files and get... (5 Replies)
Discussion started by: abhilash_menon
5 Replies

10. Shell Programming and Scripting

find files older than 30mins,count and send mail

Hi all, I wrote this script to find files older than time parameter, count the number of files, and send an email to me that some files are in a particular folder. For the particular path, the script should wait delay parameter before running again. For example, assuming the input file looks... (4 Replies)
Discussion started by: kayarsenal
4 Replies
Login or Register to Ask a Question