<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Running scripts per directory in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153825#M90685</link>
    <description>Hi,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;If you put the information in a file, then you only have to change the file when conditions change, not your script.  &lt;BR /&gt;&lt;BR /&gt;I also use a construct like:&lt;BR /&gt;&lt;BR /&gt;FILES=${1:-/$HOME/files}&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Mark</description>
    <pubDate>Wed, 05 Mar 2008 20:34:14 GMT</pubDate>
    <dc:creator>Mark Ellzey</dc:creator>
    <dc:date>2008-03-05T20:34:14Z</dc:date>
    <item>
      <title>Running scripts per directory</title>
      <link>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153818#M90678</link>
      <description>I have the below script which works fine for the purpose. I am trying to run it to get input from several other directories, &lt;BR /&gt;&lt;BR /&gt;one option is to create a script for each of these directories which will be a pain.&lt;BR /&gt;&lt;BR /&gt;The directories I am trying to run the script to get more info are:&lt;BR /&gt;&lt;BR /&gt;DIR: /tmp.. I have about 14 directories with names such as TEST_GQ3.., TEST_GX5.. etc&lt;BR /&gt;FILE: /scripts/custom also have directories GQ3, GX5 etc &lt;BR /&gt;and finally the SUBJECT of the email, I would want that to match the directory.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;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 &lt;BR /&gt;&lt;BR /&gt;and less messy way... trying not to change the original script too much though...&lt;BR /&gt;THANKS!!&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;DIR=/tmp/TEST_JB1_PREPROD/*&lt;BR /&gt;SUBJECT=TEST_JB1_PREPROD_FAILURE&lt;BR /&gt;TEXT=failed&lt;BR /&gt;FILE=/scripts/custom/JB1/save.log&lt;BR /&gt;EMAIL=vickl@excite.com&lt;BR /&gt;CHECK1=`grep -i $TEXT $DIR`&lt;BR /&gt;if [ "${CHECK1}x" = "x" ]&lt;BR /&gt;then&lt;BR /&gt;exit 1&lt;BR /&gt;fi&lt;BR /&gt;CHECK2=`find $FILE -mtime 0`&lt;BR /&gt;if [ "${CHECK2}x" != "x" ]&lt;BR /&gt;then&lt;BR /&gt;exit 2&lt;BR /&gt;fi&lt;BR /&gt;find $DIR -type f | xargs grep -i $TEXT &amp;gt; $FILE&lt;BR /&gt;mailx -s "$SUBJECT" $EMAIL &amp;lt; $FILE&lt;BR /&gt;exit 0</description>
      <pubDate>Mon, 03 Mar 2008 03:20:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153818#M90678</guid>
      <dc:creator>Vic S. Kelan</dc:creator>
      <dc:date>2008-03-03T03:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: Running scripts per directory</title>
      <link>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153819#M90679</link>
      <description>&lt;!-- !*# --&gt;&lt;P&gt;&amp;gt;one option is to create a script for each of these directories which will be a pain.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;DIR: /tmp.. I have about 14 directories with names such as TEST_GQ3.., TEST_GX5.. etc&lt;BR /&gt;&lt;BR /&gt;You want to use?&lt;BR /&gt;DIR=/tmp/TEST_*/*&lt;BR /&gt;&lt;BR /&gt;&amp;gt;FILE: /scripts/custom also have directories GQ3, GX5 etc&lt;BR /&gt;&lt;BR /&gt;So the name here is TEST_*_PREPROD?&lt;BR /&gt;&lt;BR /&gt;&amp;gt;finally the SUBJECT of the email, I would want that to match the directory.&lt;BR /&gt;&lt;BR /&gt;Which, GQ3 or longer?&lt;BR /&gt;&lt;BR /&gt;&amp;gt;trying not to change the original script too much though.&lt;BR /&gt;&lt;BR /&gt;You're fixing it, so it's better. :-)&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;TEXT=failed&lt;BR /&gt;EMAIL=vickl@excite.com&lt;BR /&gt;# the following line needs all 14 names&lt;BR /&gt;for dir in GQ3 GX5 ... ; do&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DIR=/tmp/TEST_${dir}_PREPROD/*&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SUBJECT=TEST_${dir}_PREPROD_FAILURE&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FILE=/scripts/custom/${dir}/save.log&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CHECK1=$(grep -i "$TEXT" $DIR)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if [ "${CHECK1}x" = "x" ]; then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; exit 1&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fi&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CHECK2=$(find $FILE -mtime 0)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if [ "${CHECK2}x" != "x" ]; then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; exit 2&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fi&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; find $DIR -type f -exec grep -i "$TEXT" + &amp;gt; $FILE&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mailx -s "$SUBJECT" $EMAIL &amp;lt; $FILE&lt;BR /&gt;done&lt;/P&gt;</description>
      <pubDate>Sat, 17 Sep 2011 20:46:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153819#M90679</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2011-09-17T20:46:30Z</dc:date>
    </item>
    <item>
      <title>Re: Running scripts per directory</title>
      <link>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153820#M90680</link>
      <description>Something like this might work&lt;BR /&gt;&lt;BR /&gt;#/usr/bin/sh&lt;BR /&gt;# put all 14 directories in the DIRLIST var&lt;BR /&gt;DIRLIST="GQ3 GX5 ..."&lt;BR /&gt;for BASEDIR -n $DIRLIST&lt;BR /&gt;do&lt;BR /&gt;  DIR=/tmp/TEST_$BASEDIR_PREPROD&lt;BR /&gt;  SUBJECT=TEST_$BASEDIR_PREPROD_FAILURE&lt;BR /&gt;  TEXT=failed&lt;BR /&gt;  FILE=/scripts/custom/$BASEDIR/save.log&lt;BR /&gt;# put rest of script here &lt;BR /&gt;# replace exit 1 and exit 2 with "continue"&lt;BR /&gt;# leave out the exit 0 statement&lt;BR /&gt;# add the statement "done" at end&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 03 Mar 2008 04:20:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153820#M90680</guid>
      <dc:creator>TTr</dc:creator>
      <dc:date>2008-03-03T04:20:56Z</dc:date>
    </item>
    <item>
      <title>Re: Running scripts per directory</title>
      <link>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153821#M90681</link>
      <description>&lt;P&gt;&amp;gt;TTr: for BASEDIR -n $DIRLIST; do&lt;BR /&gt;&lt;BR /&gt;Typo, that should be: in&lt;BR /&gt;&lt;BR /&gt;&amp;gt;DIR=/tmp/TEST_$BASEDIR_PREPROD&lt;BR /&gt;&amp;gt;SUBJECT=TEST_$BASEDIR_PREPROD_FAILURE&lt;BR /&gt;&lt;BR /&gt;If variables are embedded, you need ${} as in my example.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;# replace exit 1 and exit 2 with "continue"&lt;BR /&gt;&lt;BR /&gt;Ah, right if you need to do all 14.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Sep 2011 20:44:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153821#M90681</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2011-09-17T20:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: Running scripts per directory</title>
      <link>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153822#M90682</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;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!&lt;BR /&gt;&lt;BR /&gt;Construct an outer loop to read the directory names, performing the remainder of your logic within the loop:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;while read DIRNAME&lt;BR /&gt;do&lt;BR /&gt;     { code here... }&lt;BR /&gt;done &amp;lt; $0.data&lt;BR /&gt;exit 0&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 03 Mar 2008 13:03:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153822#M90682</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-03-03T13:03:16Z</dc:date>
    </item>
    <item>
      <title>Re: Running scripts per directory</title>
      <link>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153823#M90683</link>
      <description>Oh, I fa(s)t fingered that one. Thanks Dennis.</description>
      <pubDate>Mon, 03 Mar 2008 13:24:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153823#M90683</guid>
      <dc:creator>TTr</dc:creator>
      <dc:date>2008-03-03T13:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: Running scripts per directory</title>
      <link>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153824#M90684</link>
      <description>Thanks all!&lt;BR /&gt;&lt;BR /&gt;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!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 03 Mar 2008 18:22:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153824#M90684</guid>
      <dc:creator>Vic S. Kelan</dc:creator>
      <dc:date>2008-03-03T18:22:56Z</dc:date>
    </item>
    <item>
      <title>Re: Running scripts per directory</title>
      <link>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153825#M90685</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;If you put the information in a file, then you only have to change the file when conditions change, not your script.  &lt;BR /&gt;&lt;BR /&gt;I also use a construct like:&lt;BR /&gt;&lt;BR /&gt;FILES=${1:-/$HOME/files}&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Mark</description>
      <pubDate>Wed, 05 Mar 2008 20:34:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/running-scripts-per-directory/m-p/4153825#M90685</guid>
      <dc:creator>Mark Ellzey</dc:creator>
      <dc:date>2008-03-05T20:34:14Z</dc:date>
    </item>
  </channel>
</rss>

