Foreach loop through subdirectories in csh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Foreach loop through subdirectories in csh
# 15  
Quote:
Originally Posted by saleheen
Thank you so much bakunin. If I'm understanding correctly, you're telling me to use the absolute path in my script.
Actually i am telling you to use only absoulte paths in all your scripts.

Quote:
Originally Posted by saleheen
When I say
Code:
set dirs=`find . -type d`
foreach dir ($dirs)
cd $dir

shouldn't it go to the child directories where my input files are?
Yes, it should. I looked at your script in post #1 and there it reads:

Quote:
Originally Posted by saleheen
Code:
#!/bin/csh
set dir=`*/*/*/*/`
foreach $dir
 /scratch/scratchdirs/sol/Scripts/QMcalc/GEN_POSCAR
end

I take it that has already been dealt with, yes?


Quote:
Originally Posted by saleheen
Normally what I do is I go to each child directory one by one and call my script like this:
Code:
/scratch/scratchdirs/saleheen/Scripts/My-scripts/GEN_POSCAR_Ru-444

OK. What i suggest you should do is to give the directory you would go as parameter to the script. The script itself would then go to the directory, do what it has to do, and so on. I would write your script this way (all the seds put together, not used "-i" because it is not portable):

Code:
#! /bin/bash
typeset WDir="$1"
typeset WorkFile1="${WDir}/input"
typeset WorkFile2="${WDir}/temp"
typeset WorkFile3="${WDir}/mols"

                                        # a little check up front:
if [ ! -d "$WDir" ] ; then
     echo "ERROR: $WDir is not a directory, exiting..." >&2
     exit 1
fi

mv "${WDir}*.cell" "$WorkFile1"
dos2unix -q "$WorkFile1" >/dev/null 2>/dev/null 

sed  '1,7 d                   
      /%ENDBLOCK POSITIONS_FRAC/,$ d                          
      /Ru/,$ d' "$WorkFile1" > "${WorkFile1}.tmp"
mv "${WorkFile1}.tmp" "${WorkFile1}"

grep C "$WorkFile1"   > "$WorkFile2"                                          
grep O "$WorkFile1" >> "$WorkFile2"                             

sed '/C/,$d' "${WorkFile1}" > "${WorkFile1}.tmp"
mv "${WorkFile1}.tmp" "${WorkFile1}"                               

cat "$WorkFile2" "$WorkFile1" > "$WorkFile3"                                 

rm "$WorkFile2" "$WorkFile1"
....

etc., etc.. Notice that the names of the 3 files you use are defined only once in the whole script, so it is easy to change them. Also notice, that because they are all declared with a full path the script only uses absolute paths now.

/PS: Furthermore, the script now understands the directory to work on as a parameter, so instead of the (inherently error-prone)

Code:
for dir in (...some list...) ; do
     cd $dir
     script
done

you can do the (much cleaner)
Code:
for dir in (...some list...) ; do
     script $dir
done

You might post an example for the ".cells"-files you work on because i sense that for what you do there should be an easier solution than the one you found. Most probably some lines of awk will do everything your script does but ten times faster.

My suggestion regarding shells is: NEVER EVER USE CSH - it can't be stressed enough. csh is unpredictable as hell and strongly discouraged for interactive use, for scripting it is outright verboten!

If you want to write shell scripts use a POSIX-compatible shell. There are two: Korn shell (ksh) and Bourne-Again-Shell (bash). IMHO Korn Shell is better suited for scripting, but this may be personal taste as much as professional opinion. Any of the two will be suited for what your needs seem to be. It should be mentioned that the two shells languages differ only in details and if you know one you know most of the other. For what i think you want to do they are almost identical.

I do not know any bash books. For Korn Shell i can wholeheartedly recommend Barry Rosenbergs book "Hands-On KornShell93 Programming".

I hope this helps.

bakunin

Last edited by bakunin; 07-30-2014 at 05:14 AM..
This User Gave Thanks to bakunin For This Post:
# 16  
Thanks you so much bakunin, we had our eid celebrations yesterday, that's why I'm posting late. I have changed my shell into bash so I think life will be a little bit easier even for a newbie like me. Thanks again and eid mubarak Smilie
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #906
Difficulty: Easy
A Unix shell repeatedly prints a prompt, waits for a command line on stdin, and then carries out some action, as directed by the contents of the command line.
True or False?

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. 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

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

foreach loop + 2 variables

In a foreach loop, is it possible for the loop to go through 2 arguments instead of one i.e. instead of foreach i (do stuff for i), we have foreach i j(do stuff for i; do stuff for j) I am working under BASH and TCSH shell environments cheers (3 Replies)
Discussion started by: JamesGoh
3 Replies

8. 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

9. 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

10. UNIX for Dummies Questions & Answers

foreach loop question

Hello, I am new at this forum so please bare with me on this. Within a given directory, I have a list of files in which in each file, I would like to do a substitution. I would like to substitute the string mlcl to mll in each file using the foreach command. I dont quite get how to do that. If... (7 Replies)
Discussion started by: clipski
7 Replies

Featured Tech Videos