- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Running scripts per directory
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2008 07:20 PM
03-02-2008 07:20 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2008 07:52 PM - edited 09-17-2011 01:46 PM
03-02-2008 07:52 PM - edited 09-17-2011 01:46 PM
Solution>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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2008 08:20 PM
03-02-2008 08:20 PM
Re: Running scripts per directory
#/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2008 09:23 PM - edited 09-17-2011 01:44 PM
03-02-2008 09:23 PM - edited 09-17-2011 01:44 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2008 05:03 AM
03-03-2008 05:03 AM
Re: Running scripts per directory
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...
- Tags:
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2008 05:24 AM
03-03-2008 05:24 AM
Re: Running scripts per directory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2008 10:22 AM
03-03-2008 10:22 AM
Re: Running scripts per directory
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2008 12:34 PM
03-05-2008 12:34 PM
Re: Running scripts per directory
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