Foreach loop through subdirectories in csh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Foreach loop through subdirectories in csh
# 8  
Old 07-29-2014
The first line is
Code:
#!/bin/bash

Is it the problem? I tried changing that to csh and then even directly the script doesn't work. I don't know why I used bash at that time though :/
The echo testing works. Thanks again man, appreciate it.

---------- Post updated at 05:44 AM ---------- Previous update was at 05:28 AM ----------

When I tried to change the script to csh it says something like this -
Code:
mv: No match.
sed: can't read input: No such file or directory
sed: can't read input: No such file or directory
sed: can't read input: No such file or directory
grep: input: No such file or directory
grep: input: No such file or directory
sed: can't read input: No such file or directory
cat: input: No such file or directory
rm: cannot remove `input': No such file or directory
Illegal variable name.
mv: No match.
sed: can't read input: No such file or directory
sed: can't read input: No such file or directory
sed: can't read input: No such file or directory
grep: input: No such file or directory
grep: input: No such file or directory
sed: can't read input: No such file or directory
cat: input: No such file or directory
rm: cannot remove `input': No such file or directory
Illegal variable name.
./abc/OPT-0: No such file or directory.

I checked all the input files and they're correct. :/

---------- Post updated at 06:12 AM ---------- Previous update was at 05:44 AM ----------

If you want to look at my script just in case:
Code:
mv *.cell input
dos2unix -q input # > /dev/null 2> /dev/null 
sed -i '1,7d' input                   
sed -i '/%ENDBLOCK POSITIONS_FRAC/,$d' input                          
sed -i '/Ru/,$d' input               
grep C input > temp                                          
grep O input >> temp                             
sed -i '/C/,$d' input                                 
cat temp input > mols                                 
rm temp input                                               
count_C=$(grep -c C mols)
count_H=$(grep -c H mols)
count_O=$(grep -c O mols)
value="   ${count_C}     ${count_O}     ${count_H}"
sed -i 's/  H   /   /g;s/  C   /   /g;s/  O   /   /g' mols              
sed -i '1,$ s/   /  /g' mols                                                              
cp /scratch/scratchdirs/saleheen/Project-005.11.07.14.EL.44/Ru_001/VCS-S00005-001-Ru001_444R/CONTCAR .
sed -i '74,$d' CONTCAR                                 
sed -i '26,41 s/T/F/g' CONTCAR                 
cat CONTCAR mols > POSCAR                             
sed -i '74,$ s/$/   T   T   T/g' POSCAR 
rm CONTCAR mols
sed -i '6,6 s/$/   C    O    H/g' POSCAR
awk -v value="$value" 'NR==7{$0=$0" "value}1' POSCAR > Temp
mv Temp POSCAR
#qsub job-hopper-vasp

# 9  
Old 07-29-2014
Assignments like count_C=$(grep -c C mols) do not work in csh (csh would need the set keyword before).
So the shebang #!/bin/bash is correct (of course it must exist).
Your script expects to be started in a certain directory.
So put a cd command before you invoke your script,
or change your script to do the necessary cd first!
This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 07-29-2014
The scripts name is GEN_POSCAR, so if I say like this-
Code:
cd /scratch/scratchdirs/saleheen/Scripts/My-scripts/GEN_POSCAR

won't it say it's not a directory? Or am I understanding something wrong here? :/

---------- Post updated at 07:38 AM ---------- Previous update was at 07:01 AM ----------

And I should add if I put the .cell file in the same directory with the forloop script it does the work as usual which probably means it's not going to the directories cause it shows the following error message :
Code:
./abc/OPT-0: No such file or directory.

But these directories exist there :/
# 11  
Old 07-29-2014
Quote:
Originally Posted by saleheen
The scripts name is GEN_POSCAR, so if I say like this-
Code:
cd /scratch/scratchdirs/saleheen/Scripts/My-scripts/GEN_POSCAR

won't it say it's not a directory? Or am I understanding something wrong here? :/
If the scripts (short) name is "GEN_POSCAR", then it is a file in directory "/scratch/scratchdirs/saleheen/Scripts/My-scripts". The full name would be "/scratch/scratchdirs/saleheen/Scripts/My-scripts/GEN_POSCAR",

but you were told by MadeInGermany to do this:
Code:
# cd /scratch/scratchdirs/saleheen/Scripts/My-scripts
# ./GEN_POSCAR

As i sense there is some confusion about the directory concept, here it is:

Every file is placed somewhere under the "root"-hierarchy, which is denominated by "/". "/" is the topmost directory on every UNIX- (and UNIX-like) system. Now, every directory can hold two (in fact there are more, i am simplifying here) types of entries: other directories and files. Each of these other directories can also hold directories and files, and so forth. Every directory si separated by the directory "above" by a slash "/".

Therefore, "/scratch/scratchdirs/saleheen/Scripts/My-scripts" means: in the root directory "/", there is a directory "scratch". Inside "/scratch" there is another directory "scratchdirs", hence: "/scratch/scratchdirs". Inside "/scratch/scratchdirs" there is the directory "salaheen" and inside this is "Scripts", asf.. Inside the bottom directory "/scratch/scratchdirs/saleheen/Scripts/My-scripts" is a file called "GEN_POSCAR", which holds your scripts source code.

Think of it like that: you open the topmost box. Inside you find several smaller boxes (subdirectories) and some loose sheets of paper (files). Opening one of the smaller boxes you find again smaller boxes (sub-subdirectories) and other loose sheets of paper. And so on, until a box contains only loose sheets and no other boxes.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 12  
Old 07-29-2014
Thanks a lot for clearing that up for me, bakunin. I was just wondering, if I do cd scratch... and then ./GEN _POSCAR thing doesn't that mean I am running the script in the script's directory? Shouldn't I try to run the script in my current directory tree?
I did what you said but still it shows the same error message. I am curious, the first line of the error message
Code:
mv: cannot stat `*.cell': No such file or directory

why does it say that? I have the .cell file in my directory tree! Thanks a lot man! Really appreciate what you are doing.

Last edited by rbatte1; 07-29-2014 at 01:20 PM.. Reason: Added ICODE tags for inline code.
# 13  
Old 07-29-2014
Quote:
Originally Posted by saleheen
I did what you said but still it shows the same error message. I am curious, the first line of the error message
Code:
mv: cannot stat `*.cell': No such file or directory

why does it say that? I have the .cell file in my directory tree!
The error message is coming from inside the script and it is, because where the script is there is no such file as "*.cell". Note that "*.cell" is meaning all the files in the current directory with a name ending in ".cell". But what "current directory" means depends on where you were when you started the script.

This is why you should always use absolute paths (paths starting from the root dir) in scripts. For instance:

Code:
#! /bin/bash
typeset WorkPath="/path/to/some/dir"
typeset File1="${WorkPath}/filename.ext"

echo "WorkPath is: $WorkPath"
echo "Filename is: $File1"

# now, notice the usage of the variable holding the path and the other
# variable holding the filename:
cp /etc/hosts "$WorkPath"
mv "$WorkPath/ls" "$File1"

# here we do something with the file. What we do doesn't matter, just to
# show the principle
wc -c "$File1" > "$File1".tmp
mv "$File1.tmp" "$File1"

exit 0

Notice, that the full path is only named once, then used throughout the script. Think about what content each of the variables holds at which time and what the complete commands with expanded variables would look like. I suggest you adapt the script a bit and play around with it.

Having said this, you cycle through all directories and call the script for each directory once, but the directories you cycle through are nowhere mentioned in your script. You probably should do something with the directory names you cycle through, otherwise there is no sense in doing this, no?

What you do is:

Code:
./dir1: script
./dir2: script
./dir2/dir1: script
./dir2/dir2: script
...

What you probably want to do is something like:

Code:
./dir1: script ./dir1
./dir2: script ./dir2
./dir2/dir1: script ./dir2/dir1
./dir2/dir2: script ./dir2/dir2
...

Supposing that the script can do something with the directory name it gets passed. To understand how to do something with passed parameters you should read the man page of your shell (mind you, "bash" or "ksh", but DEFINITELY NOT csh! Do not even consider to use csh for scripts, the damned thing is buggy to no end!).

Here is a short introduction:

Code:
#! /bin/bash
# demonstration script for parameters
typeset localvar=""

echo "my first passed parameter is: $1"

# using variables to take parameters:
localvar="$1"

echo "you see, localvar now holds the same value: $
localvar"

exit 0

save this to a file "myparam.sh", slap on the executable-bit and issue:

Code:
./myparam.sh abc
./myparam.sh abc def
./myparam.sh "abc def"
./myparam.sh ./path/to/some/file

You might want to change your script accordingly.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 14  
Old 07-29-2014
Thank you so much bakunin. If I'm understanding correctly, you're telling me to use the absolute path in my script. I actually didn't get this part of your answer, I'm so sorry, I'm just a newbie.
Quote:
Having said this, you cycle through all directories and call the script for each directory once, but the directories you cycle through are nowhere mentioned in your script. You probably should do something with the directory names you cycle through, otherwise there is no sense in doing this, no?
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? 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

The same way I wrote in the script. When I'm setting my subdirectpry tree as the variable and calling that variable through the loop one by one, why do I have to mention directory name? And this part-
Quote:
What you do is:


Code:
./dir1: script ./dir2: script ./dir2/dir1: script ./dir2/dir2: script ...
What you probably want to do is something like:


Code:
./dir1: script ./dir1 ./dir2: script ./dir2 ./dir2/dir1: script ./dir2/dir1 ./dir2/dir2: script ./dir2/dir2 ...
What does that mean? What my understanding is ./dir1 , going into directory1 and then run the script. why there's ./dir1 at the end too? I'm so sorry man, may be I should read something before just posting my problems. Since I'm stuck with csh (Trying to go to bash) would you be kind enough to provide some easier books on csh for beginners like me?

Thank you so much again!
 
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