Operating System - Linux
1827866 Members
2037 Online
109969 Solutions
New Discussion

Running scripts per directory

 
SOLVED
Go to solution
Vic S. Kelan
Regular Advisor

Running scripts per directory

I have the below script which works fine for the purpose. I am trying to run it to get input from several other directories,

one option is to create a script for each of these directories which will be a pain.

The directories I am trying to run the script to get more info are:

DIR: /tmp.. I have about 14 directories with names such as TEST_GQ3.., TEST_GX5.. etc
FILE: /scripts/custom also have directories GQ3, GX5 etc
and finally the SUBJECT of the email, I would want that to match the directory.


As I said, if I create the script 14 times and rename the directory in the script it will work but trying to find an easier

and less messy way... trying not to change the original script too much though...
THANKS!!

#!/bin/sh
DIR=/tmp/TEST_JB1_PREPROD/*
SUBJECT=TEST_JB1_PREPROD_FAILURE
TEXT=failed
FILE=/scripts/custom/JB1/save.log
EMAIL=vickl@excite.com
CHECK1=`grep -i $TEXT $DIR`
if [ "${CHECK1}x" = "x" ]
then
exit 1
fi
CHECK2=`find $FILE -mtime 0`
if [ "${CHECK2}x" != "x" ]
then
exit 2
fi
find $DIR -type f | xargs grep -i $TEXT > $FILE
mailx -s "$SUBJECT" $EMAIL < $FILE
exit 0
7 REPLIES 7
Dennis Handly
Acclaimed Contributor
Solution

Re: Running scripts per directory

>one option is to create a script for each of these directories which will be a pain.

The right option is to either provide the directories on the command line or to hard code a directory list or search in the script.

>DIR: /tmp.. I have about 14 directories with names such as TEST_GQ3.., TEST_GX5.. etc

You want to use?
DIR=/tmp/TEST_*/*

>FILE: /scripts/custom also have directories GQ3, GX5 etc

So the name here is TEST_*_PREPROD?

>finally the SUBJECT of the email, I would want that to match the directory.

Which, GQ3 or longer?

>trying not to change the original script too much though.

You're fixing it, so it's better. :-)

#!/bin/sh
TEXT=failed
EMAIL=vickl@excite.com
# the following line needs all 14 names
for dir in GQ3 GX5 ... ; do
    DIR=/tmp/TEST_${dir}_PREPROD/*
    SUBJECT=TEST_${dir}_PREPROD_FAILURE
    FILE=/scripts/custom/${dir}/save.log
    CHECK1=$(grep -i "$TEXT" $DIR)
    if [ "${CHECK1}x" = "x" ]; then
       exit 1
    fi
    CHECK2=$(find $FILE -mtime 0)
    if [ "${CHECK2}x" != "x" ]; then
       exit 2
    fi
    find $DIR -type f -exec grep -i "$TEXT" + > $FILE
    mailx -s "$SUBJECT" $EMAIL < $FILE
done

TTr
Honored Contributor

Re: Running scripts per directory

Something like this might work

#/usr/bin/sh
# put all 14 directories in the DIRLIST var
DIRLIST="GQ3 GX5 ..."
for BASEDIR -n $DIRLIST
do
DIR=/tmp/TEST_$BASEDIR_PREPROD
SUBJECT=TEST_$BASEDIR_PREPROD_FAILURE
TEXT=failed
FILE=/scripts/custom/$BASEDIR/save.log
# put rest of script here
# replace exit 1 and exit 2 with "continue"
# leave out the exit 0 statement
# add the statement "done" at end
done

However, your CHECK1 is kind of expensive, it has to go through too many files to grep. A better approach would be to let the second "find" command do the search, redirect the output to a different file and email only if that file was not empty and mv that file to $FILE.





Dennis Handly
Acclaimed Contributor

Re: Running scripts per directory

>TTr: for BASEDIR -n $DIRLIST; do

Typo, that should be: in

>DIR=/tmp/TEST_$BASEDIR_PREPROD
>SUBJECT=TEST_$BASEDIR_PREPROD_FAILURE

If variables are embedded, you need ${} as in my example.

># replace exit 1 and exit 2 with "continue"

Ah, right if you need to do all 14.

James R. Ferguson
Acclaimed Contributor

Re: Running scripts per directory

Hi:

Instead of providing the directories on the command line or to hard coding the list in a script variable, _read_ the directory names from a _file_. This obviates the need to change your script when your requirements change!

Construct an outer loop to read the directory names, performing the remainder of your logic within the loop:

#!/usr/bin/sh
while read DIRNAME
do
{ code here... }
done < $0.data
exit 0

In the example above, the name of the file containing your directory names is the _same_ as the script name with the suffix '.data' appended. This is one way to associate the data and the script.

You have the ability to use 'continue' statements to stop the current iteration of the read loop and begin processing with the next directory name just as you would have with the 'for' loop, too.

Regards!

...JRF...

TTr
Honored Contributor

Re: Running scripts per directory

Oh, I fa(s)t fingered that one. Thanks Dennis.
Vic S. Kelan
Regular Advisor

Re: Running scripts per directory

Thanks all!

I used a combo of Dennis and TTr's and it works great, thanks also to James, looking at using that for some improvements have a nice week!


Mark Ellzey
Valued Contributor

Re: Running scripts per directory

Hi,

As James said, use a file for your various directories. I use this technique all the time on scripts that need to look at a variety of directories that are not closely related.

If you put the information in a file, then you only have to change the file when conditions change, not your script.

I also use a construct like:

FILES=${1:-/$HOME/files}

This allows me to indicate a single directory or alternate source file from the command line. Real handy for one-off runs or for testing.

Regards,
Mark