Solved

Run a specific shell script in a sub folders of current

Posted on 2011-09-21
11
190 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bash script - Exit out of choice loop 2 44
Linux Samba using Kerberos to Auth from Active Directory 9 73
Moving from Mcrypt to OpenSSL 18 48
Internal CA server 6 51
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

821 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