2

I'm using this script to run a command within each subdirectory in the 'sites' directory, EXCLUDING the 'all' subdirectory. However, when I run this, the 'all' subdirectory is still being used, even though I use an if statement to exclude it.

for dir in ~/htdocs/drupal/drupal/sites/*
  do
  if [ $dir = "/local/users/drupadm/htdocs/drupal/drupal/all" ]
    then
    continue
  fi
echo $dir
(cd $dir && /opt/webstack/php/5.2/bin/php /local/users/drupadm/drush/drush.php $1)
done

Bryan

1
  • Always quote your "$variables" Commented Jan 10, 2013 at 1:11

4 Answers 4

8

You left out the sites sub-directory:

if [ "$dir" = /local/users/drupadm/htdocs/drupal/drupal/sites/all ]
Sign up to request clarification or add additional context in comments.

Comments

0
IFS=$'\n'
for dir in `find ~/htdocs/drupal/drupal/sites/ -maxdepth 1 -type d ! -name all -printf '%p\n'`
do
    echo $dir
    (cd $dir && /opt/webstack/php/5.2/bin/php /local/users/drupadm/drush/drush.php $1)
done
IFS=' '

Exclude that directory in the first place, use find!

It's invoked by

find dir_name [tests...]

Tests used by my command:

-maxdepth 1

Of immediate children...

-type d

... find directories ...

! -name all

... Not named 'all'.

6 Comments

Don't use find `this way`. Breaks on whitespace in filenames, you know.
Agree. Recommend find ... | while read dir; do ...; done instead.
The box I'm on runs SunOS and the find command doesn't support the -maxdepth switch...
1.) If whitespace is an issue, you can always set IFS=$'\n', and then use the action -printf '%p\n' at the very end of all that 2.) Emulating max depth it trickier - I'll check the man page and get back.
@BryanChristopheGreen Okay, found an (almost) workaround: This eliminates hidden folders and is a bit messier, but try find ~/htdocs/drupal/drupal/sites/* -type d ! -name all ! -path */all/* -prune The added test is ! -path (path name not like), and I've used an action at the end, which tells you what to do when you get a match, -prune means 'stop searching downward'.
|
-1

Try quoting the $dir, you will fine directories with spaces in their names kill you. You may also run scripts with -vx switches on the invocation to see what's read in and what's executed. bash -vx my script or set -vx whatever.

1 Comment

Hey ubfan. This should be a comment since it doesn't directly answer the question.
-1

Hi you need to do the following

if [ $dir == "/local/users/drupadm/htdocs/drupal/drupal/all" ]

Thanks

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.