Solved

Run a specific shell script in a sub folders of current

Posted on 2011-09-21
11
194 Views
Last Modified: 2012-08-14
I need a script that scans all the sub folders of the current and executes a script inside. However it needs to CD into that directory before executing.

So something like this (but I know its not correct syntax):

#/!bin/bash
for file in `find . -name 'stop_services.sh'`
do
  run_script=`cat $file`
  cd directory
    $run_script
 cd ..
done
0
Comment
Question by:skione
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36573370
cd $(basename $file)

or

cd `basename $file`

if you prefer...

wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36573400
Sorry,

confused two tools. It's

cd $(dirname $file)

or

cd `dirname $file`



By the way,

the rest of your script will only work if "stop_services.sh" contains the name of an executable script, not the code itself!

Should you just want to run "stop_services.sh" in the directory where it was found in run

$file

Instead of

 run_script=`cat $file`
    $run_script



0
 

Author Comment

by:skione
ID: 36573441
stop_services actually executes a script that scans the local folder for a bunch of sub folders, cats out the file thereing to get the pid of the services it is stopping. Then end result here will be to have 2 scripts, 1 that launches all the daemons in all the subfolders and another that stops all the daemons in all the sub folders. Hopefully I will schedule a reset once per day. The daemons are written in PHP (using a PEAR Daemon package) and I want to mitigate PHP's terrible garbage collection.

So does this look right?

#/!bin/bash
for file in `find . -name 'stop_services.sh'`
do
  cd $(dirname $file)
  $file
  cd ..
done
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36573472
Nearly.

cd .. will go up only one level. If you descended more than one level before, then cd .. won't bring you back where you came from.

Although this doesn't really matter here (cd $(dirname ...) will always get you to the right place), you should use

cd -

For consistency, if you write $(dirname...) you should also write

for file in $(find . -name 'stop_services.sh')

wmp
0
 

Author Comment

by:skione
ID: 36573495
Well the script will scan all the subfolders.

CD into that subfolder

Go back up one level

CD into the next subfolder.

So if you are saying I do not need to purposefully go back up (that is the script doens't loose its context when it changes directory) that is something I did not know.

Also I forgot I need to ignore a specific subfolder (.daemon-templates)
0
 
LVL 84

Expert Comment

by:ozo
ID: 36573506
since the dirname from find . will begin with .
cd will get you to the right place only if you start from the same place that the find did
0
 
LVL 84

Expert Comment

by:ozo
ID: 36573525
or you could
wd=$(pwd)
...
cd $wd
or use
 find $(pwd)
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 36573556

Better use indeed "cd -". It will take you back where you came from.

You can omit this if you make "find" context-free, i.e. don't use "find . ..." but "find /path/to/start/dir ..."


#/!bin/bash
find . -name 'stop_services.sh' | while read file
do
  if [[ $(echo $file | grep -c ".daemon-templates")  -eq 0 ]] ; then
    cd $(dirname $file)
    $file
    cd -
  fi
done


0
 

Author Comment

by:skione
ID: 36573565
That looks good, let me test it
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36573567
Found a typo of yours in the first line, alas after clicking "Submit":

#!/bin/bash

0
 

Author Comment

by:skione
ID: 36573759
Yeah EE needs to allow editing responses. I just responded to another post and my "engrish" was horrendous!

However I would of caught that one ;)

Thanks again, I am sure I will award you the points for this, I just want to test before I close the topic.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Six Sigma Control Plans
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question