Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run a specific shell script in a sub folders of current

Posted on 2011-09-21
11
Medium Priority
?
195 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public 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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

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…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

670 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