Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Foreach loop through subdirectories in csh Post 302910995 by bakunin on Tuesday 29th of July 2014 01:52:29 PM
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:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 05:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy