Operating System - HP-UX
1752810 Members
6254 Online
108789 Solutions
New Discussion юеВ

Re: how to write script for entering into 1 or more dir and execute one script.

 
yogesh kumar_2
Frequent Advisor

how to write script for entering into 1 or more dir and execute one script.

hi

for ex. i have 1 or more directories.I want to go inside every directory and i want to execute one script.

How to write the script for the above cases?
3 REPLIES 3
piyush mathiya
Trusted Contributor

Re: how to write script for entering into 1 or more dir and execute one script.

Hi Yogesh,
It is very easy, like if you have one directory "XYZ" under your /tmp and under XYZ you have six directory A,B,C,D,E,F. and you want to switch one by one in all directory and want to execute a script, then you need to follow below steps.

cd /tmp/XYZ/
ls | while read line
do
cd $line
sh <script name>
cd ..
done

Tell me if any thing other you want. . .

Regards,
Piyush Mathiya
Rasheed Tamton
Honored Contributor

Re: how to write script for entering into 1 or more dir and execute one script.

Hi,

for i in `find /tmp/test/* -type d`
do
echo $i
cd $i
/tmp/scriptname
done

- echo $i would show the dir name
-/tmp/scriptname is the script with executable permission or replace it with sh scriptname as in Piyush's post.
Rasheed Tamton
Honored Contributor

Re: how to write script for entering into 1 or more dir and execute one script.

Or else you can first create text file contains all the dirs you want as below and manually modify as per your needs by adding more or deleting unwanted ones:

find /tmp/test/* -type d > /tmp/dirlist

Once you satisfied with the list of dirs then you can run the script on that file

for i in `cat /tmp/dirlist `
do
echo $i
cd $i
/tmp/scriptname
done