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  
Old 07-29-2014
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 04:14 AM..
This User Gave Thanks to bakunin For This Post:
# 16  
Old 07-30-2014
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
 
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