1

The script goal is simple.

I have many directory which contains some captured traffic files. I want to run a command for each directory. So I came up with a script. But I don't know why the script is run only with the first match.

#!/bin/bash
# Collect throughput from a group of directory containing capture files
# Group of directory can be specify by pattern
# Usage: ./collectThroughputList [regex]
#       [regex] is the name pattern of the group of directory

for DIR in $( ls -d $1 ); do
   if test -d "$DIR"; then
       echo Collecting throughputs from directory: "$DIR"
       ( sh collectThroughput.sh $DIR > $DIR.txt )
   fi
done
   echo Done\!

I try it with:

for DIR in $1; do

or

for DIR in `ls -d $1`; do

or

for DIR in $( ls -d "$1" ); do

or

for DIR in $( ls -d $1 ); do

But the result is the same. The for loop runs only one time.

Finally I found this one and did some tricks for it to work. However, I would like to know why my first script doesn't work.

find *Delay50ms* -type d -exec bash -c "cd '{}' && echo enter '{}' && ../collectThroughput.sh ../'{}' > ../'{}'.txt" \;

"*Delay*" is the directory pattern name that I want to run the command with. Thanks for pointing out the issues.

1

3 Answers 3

4

Since you want to find all sub-directories under $1, use it like this:

for DIR in $(find $1 -type d)
2
3

Problem

Most probably the problem you are encountering is due to the fact that you are trying to use some kind of pattern like * as argument to your script.

Running it with something like:

my_script *

What's happening here is, that the shell will expand * prior to calling your script. Thus after word splitting has been performed $1 in your script will just reference the first entry returned by ls.

Example

Given the following directory layout:

 directory_a
 directory_b
 directory_c

Calling my_script * will result in:

my_script directory_a  directory_b directory_c

being called thus your loop just iterating over $(ls -d directory_a) which in fact is nothing else but directory_a alone.

Solution

To have the program run with $1=* you would have to escape the * prior to calling your script.

Try running:

my_script \*

To see it effectively does what it is intended to do then. This way $1 in your script will contain * instead of directory_a which most probably is the way you wanted your script to work.

1
  • Thanks for pointing out the problems. However, I don't like the solution since we have to do some tricks on the use of the script. It would later causes problems to the people following the project.
    – Ha Son Hai
    Apr 5, 2013 at 14:58
1

as mikyra has pointed out, the shell expands your argument * to all entries in your directory prior to passing it to your script.

if you want shell-expansion of your wildcards (e.g. * matches all but hidden files), you could simply leave the expansion to the shell and use the result, by iterating over all arguments, rather than just the first one:

for DIR in $@; do
   # ...
done

if you want to do the expansion yourself (e.g. because the pattern should be applied only to a pre-filtered list or to files in a different directory, or because you want regex-expansion rather than shell globbing), you have to protect the argument from being expanded by the shell, either using backslash notation (like mikyra's \*) or by using quotes (which is often easier to use):

my_script "*"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.