Bash Script Help Needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script Help Needed
# 1  
Old 09-28-2016
Bash Script Help Needed

I have a small script I wrote to look for empty files on a Linux box. It finds the empty files and emails the list to me. That works great. What I need to do now is set a cron job to run the thing every 10 minutes or so (no problems there) but I don't want to get any more emails if the last email has the same contents as the last time the script ran. Does that make sense? I only want to get new email if there is a change in the number or names of the empty files it finds.

Thank you for any input. I stink at scripting of any kind.


Code:
#!/bin/bash

FQDN=`/bin/domainname -f`
FILE=/tmp/$FQDN.`date +%F`.txt
exec &> $FILE
dir=/home/
COUNT=`find $dir -type f -empty | wc -l`

if [ ! -f $FILE ]; then

echo " "
echo "The following files are empty... "
echo " "
find $dir -type f -empty
echo " "

mail -s "I Have $COUNT Empty Files"  wviands@my.email < $FILE

fi


Last edited by vgersh99; 09-28-2016 at 12:57 PM.. Reason: code tags, please!
# 2  
Old 09-28-2016
Hi,

Considering count will not help, please consider if count is same but file(name)s are different.

you could try like this:
Redirect the list of empty files to a file (better to sort ) and save it. In the next run, generate a new list and do a diff command with previous one and based on diff command exit status is not zero (0 - inputs are same , 1 - inputs are different ) then you can copy the new file to old and trigger a mail.

Please let me know how it goes.

Last edited by greet_sed; 09-28-2016 at 01:51 PM.. Reason: Update the message
This User Gave Thanks to greet_sed For This Post:
# 3  
Old 09-28-2016
Do you also want a mail if zero length files have disappeared? If not, you could send a mail only if new files have appeared ever since the last time your cron script ran.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 09-28-2016
Just looking for new files. Eventually the zero byte files will be deleted but I expect new ones to show up occasionally.

I'm still fiddling with the diff stuff.

I was spoiled earlier in life and always had a code monkey I could pester to do this stuff but now they're all gone and I'm paying for it. Working in bash from the command line is a no brainer for me but putting it into a script, well, I sometimes think my mind doesn't work that way. Might be just a lack of practice though.

Thank you for your responses.
# 5  
Old 09-29-2016
You could store the output which has been mailed once in a certain file, and always compare the contents of the new output (which would be mailed) and the old one (using a tool such as cmp), and only send the mail if they are different.
# 6  
Old 09-29-2016
I don't know how to get the email in a text file.

This is what I have at this point, still not functioning as I wish but at least I don't feel like I'm stuck in too deep a rut.

Code:
FQDN=`/bin/domainname -f`
FILE=$FQDN.`date +%F`.txt
exec &> $FILE
dir=/home/wviands
COUNT=`find $dir -type f -empty | wc -l`

find $dir -type f -empty > saved.txt 2>&1

if ! cmp -s "saved.txt" "$FILE" ; then

find $dir -type f -empty

mail -s "HFS Test Has $COUNT Empty Files"  wviands@email < $FILE

fi

# 7  
Old 09-29-2016
Don't do the exec stuff. Separate your task into two scripts.

One is calculating the message to be sent by email, but it does not actually send the mail, but writing the information to be sent to stdout.

The other script - the master script - is calling the calculating script and redirects its output to $FILE. Then it compares the ouput to saved.txt, and if they are different, it sends the mail and copies $FILE to saved.txt.
This User Gave Thanks to rovf For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Question related to bash. Your help needed.

Dear Team, i have one question related to automation of adding new users based on txt file. Here we go: I have data.txt file which shows: bang dang benc One by one, after pressing enter. And now, I need scirpt which will add those users with home directory hidden within the file... (7 Replies)
Discussion started by: protos27
7 Replies

3. Shell Programming and Scripting

Input password to bash script, save, and enter when needed

I am looking for a way to start a script and have it prompt for a password that will be used later on in the script to SSH to another host and to SFTP. I don't want the password to be hard coded. Below is my script with the actual IP's and usernames removed. #!/usr/bin/expect -f... (2 Replies)
Discussion started by: jbrass
2 Replies

4. Shell Programming and Scripting

Help needed on ssh in bash scripting

HI I have the following requirement I have a script a.sh which will deploy files in multiple servers .The argument for the a.sh is abc.gz host1.conf where abc.gz is a zip file and one.conf will contain all the database connection string . Now I have to write a b.sh which will... (7 Replies)
Discussion started by: harry00514
7 Replies

5. Homework & Coursework Questions

Linux/UNIX Bash Shell Script trouble help needed!!

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 2. Shell Bash Script 3. !/bin/bash if echo no directory then mkdir -p /home/AC_Drywall elif ; then echo "$dir already exist" fi (4 Replies)
Discussion started by: TomFord1
4 Replies

6. Shell Programming and Scripting

bash if statement help needed

Hi I need a script with an if statement that goes. I need it to search through all files within a directory with the extension .test if it finds the string '71502FSC1206' then do sed 's/71502FSC1206/\n&/g' > send.test If it finds the string '715MCH' or '715JAC' then I need it to move the... (1 Reply)
Discussion started by: firefox2k2
1 Replies

7. Shell Programming and Scripting

Help needed with bash script providing battery status

I'm really new to even doing a bash "hello world" script, so maybe someone would know how to do the following task, using bash scripting Need to login using ssh from one dell server into another dell server, and obtain the raid battery status, using dell's open manage software commands; then... (5 Replies)
Discussion started by: AJ-102111
5 Replies

8. UNIX Desktop Questions & Answers

Help needed tracing source of bash error

Issue resolved: The 'culprit file' was .bash_aliases. It had the naughty  (ASCII for the octal string Bash was detecting and returning an error about). I cleaned it up in Pico (see my post to the thread on favorite editors if you want background on why I use Pico/Nano), re-sourced it via... (0 Replies)
Discussion started by: SilversleevesX
0 Replies

9. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

10. Shell Programming and Scripting

debugging in bash!!! help needed

Hi all, Am using bash shell. Am newbie, trying to understand the debugin process of a shell script... I am unable to comprehend the control flow ,meaning from where exactly the execution of the script begins... I tried using bash-xv <scriptname> but since am new ,am finding it difficult to... (2 Replies)
Discussion started by: wrapster
2 Replies
Login or Register to Ask a Question