Script to check for few conditions and respond with outputs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to check for few conditions and respond with outputs
# 1  
Old 09-24-2013
Script to check for few conditions and respond with outputs

All,
Here is what I am trying to accomplish:
1. Check for a file of specific name recursively and when I find it,

a. if the file is more than 1hr old, no need to email or send notification, so just exit out from the shell script
b. if the file is less than 1hr old, send an email to me saying that there is a new file generated in this path and list out the path, timestamp etc
c. if the file is more than 24hrs old, delete the file and send an email that the file was deleted and then exit out of shell script.

And, here is what I have written until now.
Code:
#!/bin/ksh
>/tmp/prasad/ccorefilelist.txt
DIRECTORY=/tmp/prasad/
CCOREFILELIST=/tmp/prasad/ccorefilelist.txt
find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; >> $CCOREFILELIST
echo "find found files"
if [ -s "$CCOREFILELIST" ]
   then
tmonth=`find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; | awk '{print $6}'`
echo "The Month of the core file is $tmonth"
tdate=`find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; | awk '{print $7}'`
echo "The Date of the core file is $tdate"
ttime=`find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; | awk '{print $8}'`
echo "The Time of the core file is $ttime"
thour=`find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; | awk '{print $8}' | cut -c1-2`
echo "The Hour of the core file is $thour"
cmonth=`date +%b`
echo "The current Month is $cmonth"
cdate=`date +%d`
echo "The current Date is $cdate"
ctime=`date +%T`
echo "The current Time is $ctime"
chour=`date +%H`
echo "The current Hour is $chour"
phour=`perl -e 'use POSIX qw(strftime);  print strftime "%H", localtime(time()+$ARGV[0]);' -- -3600`
echo "The previous Hour is $phour"
yesterday=`perl -e '@T=localtime(time-86400);printf("%02d/%02d/%02d",$T[4]+1,$T[3],$T[5]+1900)'|cut -d '/' -f2`
echo "Yesterday is $yesterday"
if [[ "$tmonth" = "$cmonth" && "$tdate" = "$cdate" && "$thour" -lt "$phour" ]]
  then
        echo "New ccore files are generated today:"
        echo "However this listing of ccore files is stale and no notification will be sent through email:"
elif [[ "$tmonth" = "$cmonth" && "$tdate" = "$cdate" && "thour" -lt "$chour" ]]
  then
   echo "entering the section of within last hour"
   echo "The listing of ccore files generated within the last hour is as follows:" >> $CCOREFILELIST
   mailx -s "$SUBJECT" test@test.com < $CCOREFILELIST
   exit
elif [[ "$tdate" -lt "$yesterday" ]]
   then
     echo "The listing of ccore files generated is over a day old and hence will be deleted:"
     ####find logic to remove files older than 1 day######
fi
    else
echo "There are no ccore files to be removed"
exit
fi

I think this script still has some logic missing like checking for other ccore file that may get generated within the hour etc. I am just using this as a template, and trying to refine and make it work.

Let me know if there is a simple and a better way to accomplish this task.

Last edited by pnara2; 09-24-2013 at 06:03 PM.. Reason: typo in script
# 2  
Old 09-24-2013
Use code tags and indentation to make your code easy to read.

Regards,
Alister

EDIT P.S.: Thanks, Scrutinizer.
# 3  
Old 09-24-2013
This might help - fine each ccore, check it's age and add to files. You can then process your LT1H and OT1D files.

Code:
function age_seconds()
{
    fs=`perl -e 'use File::stat; print stat($ARGV[0])->mtime;' "$1"`
    cs=`date +%s`
    let age=cs-fs
    echo $age
}

>/tmp/LT1H
>/tmp/OT1D

find ${DIRECTORY} -type f -name "ccore" | while read file
do
   core_age=`age_seconds "$file"`
   if [ $core_age -lt 3600 ]
   then
        echo "$file" >> /tmp/LT1H
   elif [ $core_age -gt 86400 ]
   then
        echo "$file" >> /tmp/OT1D
   fi
done

# 4  
Old 09-24-2013
Some hints (not exhaustive):
- if you got more than one ccore file in all subdirectories, your tmonth etc. variables will not be unique.
- why are you using four finds to extract those four vars? Use one and extract all variables from that.
- same for the date/time vars
- depending on your find version, you could use its time tests to analyse the one, more, and 24 hour conditions for the ccore files, list and - if need be - delete them, and send mails. No need for the cumbersome shell arithmetics!

Last edited by RudiC; 09-24-2013 at 06:55 PM.. Reason: typo
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 Check Multiple conditions in IF statement?

I wish to check two conditions inside the if statement Condition 1: The two file contents should be identical // using cmp command for this. Condition 2: The two filenames should NOT be the same. This is what i did in vain. if ]; then where entry1 and entry2 are ls *.txt | while... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. Shell Programming and Scripting

Linux Shell: how to check a string whether meets some conditions

Hi, guys. In Linux Shell script, how can I check a string whether meets some conditions. e.g.: If a string str must start with a underscore or a alphabet, and it must contains at least one lowercase, one uppercase, one numeric and one punctuation, and its length must be more than 8 characters... (2 Replies)
Discussion started by: franksunnn
2 Replies

3. Shell Programming and Scripting

Assign specific Color to the outputs in script

Hi, Im programming an interactive menu that verifies the exports of my oracle DB, and im having some trouble finding a process that outputs for example a green command line when the export terminated successfully. i have something like this cat... (15 Replies)
Discussion started by: blacksteel1988
15 Replies

4. Shell Programming and Scripting

Script that outputs user logins sorted by duration

Hello, I want to write a script that takes a username as input and outputs the user's logins sorted by duration. Also I want to exclude the "still logged in" entries. I use the "last" command but Im having problems sorting the entries based on the duration. Can you help me? Thanks a lot =) (4 Replies)
Discussion started by: ddante
4 Replies

5. Shell Programming and Scripting

create outputs from other command outputs

hi friends, The code: i=1 while do filename=`/usr/bin/ls -l| awk '{ print $9}'` echo $filename>>summary.csv #Gives the name of the file stored at column 9 count=`wc -l $filename | awk '{print $1}'` echo $count>>summary.csv #Gives just the count of lines of file "filename" i=`expr... (1 Reply)
Discussion started by: rajsharma
1 Replies

6. Shell Programming and Scripting

expect script, how to deal with different outputs.

I have an expect script that I need to deal with a few different outputs? Here are the commands I am running ssh -o StrictHostKeyChecking=no root@$ipaddress "racadm config -g cfgIpmiLan -o cfgIpmiLanEnable 1" Now, the system I am running this from if I have connected to the server before I... (0 Replies)
Discussion started by: trey85stang
0 Replies

7. Shell Programming and Scripting

Script that takes in variables, and outputs to another text or script file

Ok, I sort of need to create a command files that will be ftped to another server to run. I have some input variable that will need to be read, and then transformed into another script file. Here are some examples. Server 1: outputCmd.sh passing in ./outputCmd.sh nh8oaxt Release_4_0... (1 Reply)
Discussion started by: orozcom
1 Replies

8. Shell Programming and Scripting

Trouble with tee command to capture script outputs

function GetInput { print -n "Input" read input export INPUT=$input } export COMMAND="GetInput" $COMMAND echo "$INPUT" $COMMAND | tee -a Log.log echo "$INPUT" The first one without "tee" works fine. echo "$INPUT" displays the values I type in for input. The second... (5 Replies)
Discussion started by: muthubharadwaj
5 Replies

9. Shell Programming and Scripting

problems with a script that outputs data to a file

First of all, im a total newbie to the point that i do not know what are the terms to search for my problem. I did however spend the rest of the day today trying to figure out what is wrong with my bash script. ive always thought that the best way to learn is to tackle a problem heads on. but at... (1 Reply)
Discussion started by: joeribut
1 Replies

10. Shell Programming and Scripting

Bash script on startup does not respond as expected

Hi, I have a bash script which I have referenced in the rc.local of my fedora linux OS. However it doesnt respond the same as when run in terminal from fedora. The bash script has a series of interactive questions that require user input as shown: #!/bin/bash echo "Do you want to use... (1 Reply)
Discussion started by: Crigamorfin
1 Replies
Login or Register to Ask a Question