Solved

Run a specific shell script in a sub folders of current

Posted on 2011-09-21
11
187 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now