LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2013, 05:43 PM   #1
bcgreen24
LQ Newbie
 
Registered: Jan 2013
Posts: 1

Rep: Reputation: Disabled
Unhappy Comparing Directory Name to String


Will someone PLEASE restore my sanity? 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.

Code:
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
Thanks in advance for any and all help!

Bryan
 
Old 01-10-2013, 09:05 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Add a statement to echo each value of $dir to see what the value actually is. (Move the statement you've now got to the top of the loop, just for debugging purposes.
 
Old 01-10-2013, 09:56 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
AFAIK "~/htdocs/drupal/" can never be "/local/users/" unless "${HOME}" is /local/users/? And don't use "for" loops but "while" ones and properly quote variables.
Code:
#!/bin/bash
# Set debug and error mode while testing, set default behaviour and there's a "$1" on line 8 so exit if it wasn't supplied:
set -vx; LANG=C; LC_ALL=C; export LANG LC_ALL; [ $# -eq 0 ] && { echo "Need one argument, exiting."; exit 1; }
find ~/htdocs/drupal/drupal/sites/* -type d | while read RESULT; do
 [ "${RESULT}" = "/local/users/drupadm/htdocs/drupal/drupal/all" ] || \
 { echo "${RESULT}"; ( cd "${RESULT}" && /opt/webstack/php/5.2/bin/php /local/users/drupadm/drush/drush.php "$1"); }
done; exit 0

Last edited by unSpawn; 01-10-2013 at 12:48 PM. Reason: //Added check for "/local/users/drupadm/htdocs/drupal/drupal/all".
 
Old 01-10-2013, 12:12 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Please explain the downside of for loops in this context. I use that pattern all the time and I don't think I've ever bumped into problems with it.
--- rod.

Last edited by theNbomr; 01-10-2013 at 12:18 PM.
 
Old 01-10-2013, 12:45 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Kind of OT but here goes:
Code:
mkdir theNbomr; cd theNbomr; mkdir "Please explain"; mkdir "the downside of for loops"; mkdir "in this"; mkdir context.

\ls -1Q
"context."
"in this"
"Please explain"
"the downside of for loops"

for dir in *; do \ls -1dQ $dir; done
"context."
ls: "in": No such file or directory
ls: "this": No such file or directory
ls: "Please": No such file or directory
ls: "explain": No such file or directory
ls: "the": No such file or directory
ls: "downside": No such file or directory
ls: "of": No such file or directory
ls: "for": No such file or directory
ls: "loops": No such file or directory

find . -type d | while read ITEM; do ls -1dQ "${ITEM}"; done
"./Please explain"
"./the downside of for loops"
"./in this"
"./context."

I think one can safely say that "while" loop usage should be the default reflex unless specifically requiring a "for" loop. It's one of the BaSH pitfalls a few LQ members have educated ppl about over the past years, one of them being David the H. Apart from the ABS also see http://mywiki.wooledge.org/.
 
Old 01-10-2013, 03:51 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Yeah, I see. I'm religious about the no-whitespace-in-filenames so that partly explains why I haven't been tripped up by it. I'm also fairly likely to use
Code:
for dir in *; do \ls -1dQ "$dir"; done
which seems like a simple enough solution.
Sorry to take this off topic.
--- rod.
 
Old 01-11-2013, 09:26 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
for dir in *; do \ls -1dQ $dir; done
Actually, the problem isn't the for loop. That's just fine:

globbing patterns expand last in the parsing order, after word-splitting is finished, so using them in for loops is no problem. In fact, it's recommended!

How can I find and deal with file names containing newlines, spaces or both?


The trouble here comes later on, when you try to expand the $dir variable. Now the unquoted variable gets split into individual "words", and ls gets handed a series of non-file names to try to parse.


The secret is to always ensure that your variables are double-quoted.

Code:
$ mkdir theNbomr; cd theNbomr; mkdir "Please explain"; mkdir "the downside of for loops"; mkdir "in this"; mkdir context.

$ for dir in *; do \ls -1dQ "$dir"; done
"context."
"in this"
"Please explain"
"the downside of for loops"
There are only a very few times when you'll ever want to leave quotes off, and that's only when you need the values to be word-split (and even then there are usually better techniques available). Beware also what I said about globbing too. If any of the filenames should happen to form valid globbing patterns themselves, they will also expand.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


The danger of for loops is in trying to use them to process the contents of files or command output. Then it becomes very hard to properly word-split the content so that it can be safely parsed one line or section at a time. That's when the while+read loop should be used.

Don't Read Lines With For
How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?


A large number of scripting problems come down to not clearly understanding the command parsing order, what gets done before or after what. Learn that, and half of your troubles will go away.

http://mywiki.wooledge.org/BashParser
http://wiki.bash-hackers.org/syntax/grammar/parser_exec
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Quick help with comparing string to integer on IF statement ochocobull Programming 8 04-13-2012 02:06 AM
[SOLVED] Comparing string with integer luvshines Programming 5 09-20-2010 01:23 PM
problem in comparing numeric with string naren_0101bits Programming 1 01-28-2008 08:10 AM
[php] comparing found username with string Erhnam Programming 4 03-15-2005 09:34 PM
storing and comparing a string Randall Programming 18 03-10-2003 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration