![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Auto copy for files from folder to folder upon instant writing | Bashar | UNIX for Advanced & Expert Users | 2 | 08-21-2008 03:44 PM |
| foreach loop | ROOZ | Shell Programming and Scripting | 3 | 06-05-2008 04:20 PM |
| foreach loop | abch624 | Shell Programming and Scripting | 1 | 03-19-2008 09:34 PM |
| Parse the .txt file for folder name and FTP to the corrsponding folder. | MeganP | Shell Programming and Scripting | 3 | 07-03-2007 02:54 PM |
| foreach command ?! | geoquest | UNIX for Dummies Questions & Answers | 5 | 05-20-2002 06:11 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
foreach folder
Hi, I'm having a small issue here and I can't get it to work. I'm programming a script for bash and I need to do something to all the folder in a directory. So I'm in the directory and I want to use the foreach statement but I dont know how to reference all the folders of that directory. To make things simplistic here's my code: Code:
foreach instance (.) cd instance mkdir test end basically for each folder in that directory, I want to cd into it and create a folder called test. Any ideas? |
|
||||
|
A small correction blowtorch. Code:
for file in *; do
if [ -d $file ]; then
mkdir $file/test;
fi
done
Since you had issued a cd command it would create the sub-dir only for the first directory. So the result would not be as expected. Last edited by lorcan; 08-11-2007 at 08:51 AM.. |
|
||||
|
Quote:
current directory: a b c Inside of each directory there is a file here (using a as an example): a/test/folder/script.sh Sorry for the generic names. Now what I want my script to do is access that script.sh in each folder and run it. I was gonna do the for statement and then cd into that path and then do an execute command but I guess it's not that simple. Any ideas? thanks! |
|
||||
|
You can try like Code:
for file in *; do
if [ -d $file ]; then
./$file/test/folder/script.sh;
fi
done
or if you are particular to go into the dir and execute the script then try like this Code:
scriptHome=$(pwd) # Assuming that you are running from the path where a b c are present
for file in *; do
if [ -d $file ]; then
cd $file/test/folder;
./script.sh
fi
cd $scriptHome
done
Again assuming that the tree structure test/folder is present in the directories a,b and c. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|