Foreach loop through subdirectories in csh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Foreach loop through subdirectories in csh
# 1  
Old 07-27-2014
Foreach loop through subdirectories in csh

Hi

You might find it very trivial but actually don't know how to loop through all sub-directories and their child directories into a csh. bash was easier I believe but here I am, stuck with csh. So elaborately here's my problem:
Let's say I have my parent directory named C-H/ under which I have C2H3O1/, C2H4O1/...let's say under C2H3O1/ I have several subdirectories like CH2CH2OH/, CH3CH2O/, CH3CHOH/. Inside each sub-directories I have several child directories likeVAS-S0001-001-Ru_001/,VAS-S0001-002-Ru_001/,VAS-S0001-003-Ru_001/. Inside all of them reside OPT-0/, OPT-1/ inside which my job input files and job scripts reside. I wrote a script that I'd like to run inside each child directory that modifies some input files and submits the job. I would like to run my script one time under C-H that'll go through each directory and submit my job.

Here's the silly thing that I have:
Code:
#!/bin/csh
set dir=`*/*/*/*/`
foreach $dir
 /scratch/scratchdirs/sol/Scripts/QMcalc/GEN_POSCAR
end

I know I'm doing something very very wrong, I don't have a counter for the foreach but how do I set that since the number of directories might be different for different cases? Thanks a lot!
# 2  
Old 07-27-2014
You are *really* close:
Code:
#!/bin/csh
set dirs=`*/*/*/*/`
foreach dir ($dirs)
 cd $dir
 /scratch/scratchdirs/sol/Scripts/QMcalc/GEN_POSCAR
end

This User Gave Thanks to Smiling Dragon For This Post:
# 3  
Old 07-27-2014
If you want the whole directory tree:
Code:
set dirs=`find . -type d`

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-27-2014
Thanks a lot guys! Still I'm having this problem:
Code:
/scratch/scratchdirs/sol/Scripts/QMcalc/GEN_POSCAR: Command not found.
/scratch/scratchdirs/sol/Scripts/QMcalc/GEN_POSCAR: Command not found.
./abc/OPT-0: No such file or directory.

P.S. I just created some directories named abc, def and some subdirectories like OPT-0,OPT-1 to test it. If I directly go to the directory and run my script it works, so I think the script is fine.
smiling dragon, your code:
#!/bin/csh set dirs=`*/*/*/*/` foreach dir ($dirs) cd $dir /scratch/scratchdirs/sol/Scripts/QMcalc/GEN_POSCAR endShould it be
Code:
cd $dir

or
Code:
cd $dirs

? I tried both but for
Code:
cd $dirs

it says too many arguements. Can you kindly explain why it's
Code:
cd $dir

? Again thanks guys, really appreciate this.
# 5  
Old 07-28-2014
dirs is a list of all directories.
The loop variable dir picks one directory at a time.
Test it with
Code:
echo $dirs
foreach dir ($dirs)
  echo $dir
end

We all thought that your script is /scratch/scratchdirs/sol/Scripts/QMcalc/GEN_POSCAR.
Is it different from the script that manually run?
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 07-28-2014
No, that's the same script :/
# 7  
Old 07-29-2014
Please give the shebang (1st line) of your script!
This User Gave Thanks to MadeInGermany For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Foreach: Words not parenthesized. csh

Just started shell scripting for the first time today :D Can anyone tell me why I get the error "foreach: Words not parenthesized." for my following code? The program takes in a list of arguments. foreach card ($argv) echo Hello end (3 Replies)
Discussion started by: pkuebler
3 Replies

2. UNIX for Dummies Questions & Answers

foreach loop in csh

Hi everyone I'm new to unix and encountered a small problem i couldnt find out a reason why it doesn't work..please help.. in my csh script when i tried to use the foreach loop like this: foreach x ( ls ) echo $x end when i tried to run it, it printed out 'ls' to the std out instead of... (3 Replies)
Discussion started by: ymc1g11
3 Replies

3. Shell Programming and Scripting

foreach loop problem

Dear all, I wrote a script to download files and move files in directories according to their name. Now here is the problem: Both p101 and p360 data download successfully, but when I move them according to the year and month, only p101 data can be placed at the right location, p360,... (1 Reply)
Discussion started by: handsonzhao
1 Replies

4. UNIX for Dummies Questions & Answers

Using the Foreach loop, Needing help

I am trying to make a script for my Counter-Strike: Source servers. What i am wanting it to do is for it to restart each server, the only way i can think of doing this in through for each. Years what i have at the moment. server_start() { START=`ps x | grep SCREEN | grep $SRV | cut -d '?' -f... (5 Replies)
Discussion started by: grahamn95
5 Replies

5. UNIX for Dummies Questions & Answers

Foreach loop that skips the header line of a file (csh)

Hello all, I'm working on a foreach loop to compare a couple sets of data. However, each datafile includes a header row. I'm wondering if it is possible to tell the foreach loop to skip the first line of data. I've been using the basic code as follows: foreach line ("`file.csv`") set... (2 Replies)
Discussion started by: meteorologistks
2 Replies

6. Shell Programming and Scripting

How to loop(Iterate) through List with foreach(csh)

Hey all,, I know cshell is harmful:) but I am using this just "to know" - for educational purposes!... not for a long-term use. lets say i have a list.. set arr=(x y z e f) I wanna iterate the list with foreach ,, not with while.!! foreach i $arr echo $i end does not work (2 Replies)
Discussion started by: eawedat
2 Replies

7. Shell Programming and Scripting

foreach in csh

I have a foreach loop in a csh script and I noticed that it tries to find the files with the pattern *$searchpt* in the file name. I'm confused as I never specified checking for the files. foreach f ( *$searchpt* ) set fnew = `echo $f | awk -v searchpat=$searchpt \ ... (1 Reply)
Discussion started by: kristinu
1 Replies

8. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

9. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

10. Shell Programming and Scripting

Foreach loop

What am I doing wrong with this foreach loop? foreach var ($argv) @sum = $sum + $var (4 Replies)
Discussion started by: haze21
4 Replies
Login or Register to Ask a Question