Check file size and mail


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Check file size and mail
# 1  
Old 12-02-2014
Check file size and mail

Hi,

I am trying to write a script which will check if the filesize is grather than 0 KB, compress the file and send to the email list else if the file size is zero KB don't send a mail update the log

Code:
if [ `ls -l /tmp/file.txt | awk '{print $1}'` -eq 0 ]
then
echo "Validate the file" | mailx -s " There are errors : " ${EMAIL_LIST}
else
echo "No Errors" > log_file
fi


When I am executing the script I am getting the below error
Code:
 [: -rw-r--r--: integer expression expected

Please let me know where I am doing it wrong.

And if we compress the file and email, can we extract in windows using the normal winzip software?

Thanks and Regards,
Mora
# 2  
Old 12-02-2014
Hello mora,

Kindly use as follows in if.

Code:
if [[ `ls -l /tmp/file.txt | awk '{print $1}'` -eq 0 ]]

EDIT: Also to check file size zero you can do as following.

Code:
if [[ -s /tmp/file.txt ]]


Thanks,
R. Singh

Last edited by RavinderSingh13; 12-02-2014 at 03:29 AM.. Reason: Added one more solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 12-02-2014
Thanks Ravinder for the quick reply, can you please tell me if we compress the file and send the file as attachemt can we unzip the file using winzip
# 4  
Old 12-02-2014
Hello mora,

Yes we can do it, following link may help you to understand it more.

Information about WinZip and UNIX compress files - WinZip Computing :: Knowledgebase


Thanks,
R. Singh
# 5  
Old 12-02-2014
Quote:
Originally Posted by mora
Hi,

I am trying to write a script which will check if the filesize is grather than 0 KB, compress the file and send to the email list else if the file size is zero KB don't send a mail update the log

Code:
if [ `ls -l /tmp/file.txt | awk '{print $1}'` -eq 0 ]
then
echo "Validate the file" | mailx -s " There are errors : " ${EMAIL_LIST}
else
echo "No Errors" > log_file
fi


When I am executing the script I am getting the below error
Code:
 [: -rw-r--r--: integer expression expected

Please let me know where I am doing it wrong.

And if we compress the file and email, can we extract in windows using the normal winzip software?

Thanks and Regards,
Mora
The file size in ls -l output is field 5; not field 1. Try:
if [ `ls -l /tmp/file.txt | awk '{print $5}'` -eq 0 ]
but as RavinderSingh13 suggested (but I think he reversed the desired outcome), a much more efficient test is:
Code:
if [ ! -s /tmp/file.txt ]


Last edited by Don Cragun; 12-02-2014 at 04:17 AM.. Reason: Add more efficient test.
# 6  
Old 12-02-2014
Quote:
> Please let me know where I am doing it wrong.
Well, there are few "bugs" in your code.

Let's start with the reason why you're getting the error message.

In short: You are doing the comparison on the wrong field.

The awk command you used returns the first column ($1) from the output of ls -l /tmp/file.txt.

I illustrated the different fields below
Code:
-rw-rw-r-- 1 root root   60 Dec  1 20:41 file.txt
---------- - ---- ----   -- --- -- ----- --------
    $1     $2 $3   $4    $5  $6 $7  $8      $9

Note that the fields shown here might vary from system to system and thus it is no good practice to parse the ls -l output.

If you want to stick to your approach for whatever the reason, you need to change $1 in your awk command to $5, respectively to the field number that contains the file size on your system (verify in the shell first).

The second solution RavinderSingh13 has posted is probably the best alternative.

---

Another problem is that your code says if the filesize is 0, then send mail, but it should echo "No errors" if the filesize is 0.
So either you interchange the commands in the then and else section, or you change -eq to -gt

---

You are sending the email without attachment...

Below should zip the file and attach it to the email
Code:
zip /tmp/file /tmp/file.txt
echo "Validate the file" | mailx -s " There are errors : " -a /tmp/file.zip ${EMAIL_LIST}
rm /tmp/file.zip

When using zip, you should be able to unzip it on a windows machine.

Last but not least, maybe, before zipping the file, you will need to replace the UNIX/Linux newline character with Windows' equivalent using unix2dos (https://en.wikipedia.org/wiki/Unix2dos) or something, so that the text file can be properly displayed on Windows.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File size check

I am trying to check whether two files are empty or not using below if condition but its checking for only one file if ] Again I tried if && ] Need your assistance (2 Replies)
Discussion started by: Aditya_001
2 Replies

2. Shell Programming and Scripting

Check a file size

I'm doing a script thats check if mylogfile.log is bigger then 5000 but i dont know how to write it. thanks in avance. (6 Replies)
Discussion started by: Froob
6 Replies

3. Shell Programming and Scripting

Mail file size of newest file in directory

I have been a long time lurker, and have learned a lot from these forums, thank you to everyone. I am using Zoneminder to record a security camera feed. No motion detection, just 24 hour recording. I then have a script that checks Mysql for events dated the day before, and throws them at... (4 Replies)
Discussion started by: iamVERYhungry
4 Replies

4. Shell Programming and Scripting

check the file size

if ; then cp /tmp/testfolder/*.* ~/new/logs/ else echo "No files today" exit fi The problem is this doen't work when there is more than 1 file. Please tell me how to take the latest file and check the size of the file in a directory (1 Reply)
Discussion started by: sandy1028
1 Replies

5. Shell Programming and Scripting

Check the file size - help

I want to write a batch job (ksh) with the following requirement we have file feeds coming to our system from other team, if the file size is greater than expected then we dont need to process the file for the day and need to archive the file and send email notification to the manager saying... (5 Replies)
Discussion started by: sithara
5 Replies

6. Shell Programming and Scripting

Check for file size is zero or not.

I have following script on AIX/KSH if ] ; then echo "filename exists and is > 0 bytes" else echo "filename does not exist or is zero length" fi It is not working. What is wrong here??? (3 Replies)
Discussion started by: Hangman2
3 Replies

7. Shell Programming and Scripting

To check file size

Hi All, I am in small problem.. i have one script which transfers some big files to my ftp usign normal command like put .... my problem is how to check whether my file have been transferred successfully on ftp or not... i know only inside ftp we have option like 'size' command which... (2 Replies)
Discussion started by: Shahul
2 Replies

8. UNIX for Dummies Questions & Answers

Mail based on file size

Hi Friends, i am trying to write a shell script which will check for the logfile size. if the content of the logfile is wc -l >=1 then send mail with log file as attachment.else do nothing. i'm trying the below code can any one help on this if ] then (echo "`cat... (2 Replies)
Discussion started by: rajendragora
2 Replies

9. Shell Programming and Scripting

file size check

How can I perform size check of any character file(which switch)? For example: I have to perform certain actions if file size is not zero. How can I do that? Is this syntax fine? if test ! -z $filename then fi (2 Replies)
Discussion started by: malaymaru
2 Replies

10. UNIX for Dummies Questions & Answers

Check file size

I need a unix script that will check the size of multiple files in the same directory or from a text file. (6 Replies)
Discussion started by: alnita
6 Replies
Login or Register to Ask a Question