Script to mail if files do not exist


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to mail if files do not exist
# 1  
Old 04-22-2013
Script to mail if files do not exist

Hi all!

I need some help with a script, but I want to make it better, my script prints the last two files on each directory:
Code:
echo " "
echo "******************* mediation_sgsn:***********************"
ls -lrt /moneta_collected03/mediation_sgsn | tail -2
echo " "
echo "************************* HLR:****************************"
ls -lrt /moneta_collected03/hlr | tail -2
echo " "
echo "*********************** tap_in:***************************"
ls -lrt /moneta_collected02/tap_in | tail -2

But at a certain point if there is no file I want the script to report that there is no file instead of just:
Code:
************************* HLR:****************************
total 0

can I have some help?
# 2  
Old 04-22-2013
Read more about "unix file tests".
# 3  
Old 04-22-2013
If I was looking for a particular file, that could be done by creating a variable with a filename, but in this case these directories hosts a lot of files, and if there are files I need to print the last 2
# 4  
Old 04-22-2013
why not append
Code:
| wc -l

to each line? would give you a count.
Then you could compare if that is at your threshold level.
# 5  
Old 04-22-2013
The requirement is really to print the last two lines with
Code:
ls -lrt

and not really counting files, and if there is no files in it report it
# 6  
Old 04-22-2013
Aren't you trying to see if the count is a zero?
Thus, the result of the command with the '|wc -l' appended will tell you the number of files. Then, you can either:
store that result to a variable and then analyze for >0
or
do all in one command
# 7  
Old 04-22-2013
Code:
function do_one {
  ls -lrt $1 | tail -2 > /tmp/tail.x
  if [ -s /tmp/tail.x ]; then
    echo "No files under this directory"
  else
    cat /tmp/tail.x
  fi
  }

echo " "
echo "******************* mediation_sgsn:***********************"
do_one "/moneta_collected03/mediation_sgsn"
echo " "
echo "************************* HLR:****************************"
do_one "/moneta_collected03/hlr"
echo " "
echo "*********************** tap_in:***************************"
do_one "/moneta_collected02/tap_in"

BTW: The "double quotes" around the directory names are only needed if embedded blanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to attach latest files of directories in a mail.

Hello Folks, I am looking for the script which will go to directory and check for the latest 5 files and send a mail to attaches these files to user. Kindly guide. Regards (7 Replies)
Discussion started by: sadique.manzar
7 Replies

2. UNIX for Beginners Questions & Answers

Check if 10 files exist

Hi All, Whenever i get 10 files(file names like sales*) then another file need to create. May i know how to implement this in KSH. (4 Replies)
Discussion started by: siddireddy
4 Replies

3. Shell Programming and Scripting

Script for checking files for last hour and send a mail

Hello, I need to write a script to check the files in one folder , if that folder doesn't have files generated from last 1 hr then we need to send mail to particular persons. So Can you please help me to write script to check the files and send email. Thank you.. (1 Reply)
Discussion started by: archana23
1 Replies

4. Shell Programming and Scripting

Shell script to check files if exist else touch the file

Hi All, Thanks in Advance I wrote the following code if then echo "version is 1.1" for i in "subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account" do FILE="SDP_DUMP_$i.csv" echo "$FILE" ... (5 Replies)
Discussion started by: aealexanderraj
5 Replies

5. Shell Programming and Scripting

bash script for testing existence of files/folders and creating if neither exist

Hi, I am new to shell-scripting, and doing a lot of reading. I am having some trouble getting started with a simple testing of scripting. I have been experimenting with if, loops, for, test, etc., but still unsure. I seem to have the hang of it when it comes to creating a single file or... (6 Replies)
Discussion started by: me2
6 Replies

6. Shell Programming and Scripting

Find out whether files exist.

I have the following data stored in a file. 1 /home/file13 /home/file2 2 /home/file41 /home/file654 3 /home/file61 /home/file45 4 /home/file81 /home/file43 ... I want to print the first column provided the files represented by the second and third column exist. How to do that? (3 Replies)
Discussion started by: kevintse
3 Replies

7. Shell Programming and Scripting

Delete files if they exist

In a directory a number of files named res0.om res1.om ... resN.om where N can be any unknown number between 1 and 999 Please help me filling out the gaps in the following csh script: I need to delete all files exept res0.om The easy way is rm res1* rm res2* rm res3* rm res4*... (5 Replies)
Discussion started by: pederlol
5 Replies

8. UNIX for Dummies Questions & Answers

testing if files exist

I am trying to test arguments to see if they are files in any directory. I have : but it's not working (7 Replies)
Discussion started by: skooly5
7 Replies

9. Shell Programming and Scripting

Script needed which will send a mail with attachment files

Hello, I need a shell script which need to send a mail with 4 *.csv files as attachments.Anybody can help me? At present i have a script which is sending only one file as attachments.But i need a script which send 4 files in a shot. #!/bin/ksh /bin/mail xxxx@xx.xom << EOF Subject:... (4 Replies)
Discussion started by: Mar1006
4 Replies

10. Shell Programming and Scripting

Mail Files Script

Please let me know if I'm the right track here, or how it should be done better. #!/bin/bash list=`ls -al /some/directory | grep "$1"* | awk '{print $9}'` for file in $list do mail -s "$file" me@email.com < $file done My concern is with the list variable. The output is like: ... (6 Replies)
Discussion started by: earnstaf
6 Replies
Login or Register to Ask a Question