Solved

Run a specific shell script in a sub folders of current

Posted on 2011-09-21
11
189 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
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

773 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