[ale] scripting (looping through filenames containing spaces)

Ben Phillips pynk at angband.org
Tue Jul 4 00:35:49 EDT 2000


SHORT STORY:

Fix the script below so it outputs the following:

a b c
1 2 3

It's important that the backquotes (or some other command-substition) be
there.  The idea is to get 'for' to recognize tokens that have whitespace
characters in their names.

------------------------------------------
#! /bin/bash
#
# Loop through output that contains spaces
#

 for i in `echo \"a b c\"; echo \"1 2 3\"`;
    do echo $i
 done
------------------------------------------



LONG STORY:

Yesterday I determined I was tired of having capital letters in the names of
the files on my Windows partition.  (Don't laugh.)

So I tried to use 'find' and a 'for' loop to change the names of all the
directories (thinking I'd do the filenames later).  And then I realized that
'for' just wasn't cut out to deal with tokens that have spaces in them; it
thinks a directory named "TWO WORDS" is really two items named TWO and
WORDS, and it treats them that way even when I encase them in quotes.  (I
even wrote a bit of perl that will encase each line of output in
double-quotes, so my script is taking in a list just like the above example.  
Tried it with single-quotes, too, to no avail.)

Any alternatives you can suggest for accomplishing the same thing would be
much appreciated.

Here's the full script as I have it so far; please feel free to nitpick my
scripting style.


#!/bin/bash
#
# Recursively lower-case the name of every directory in a tree
# (The perl lines are just encasing every line of input in quotes.)
#  \x22 == double quote; \x27 == single quote
#

count=0
while [ $count -lt 30 ]; do
    for i in `find $1 -maxdepth $count -type d |                          \
          perl -we '{ while(<>) { chomp; print "\x27$_\x27\n" } }'`;
       do mv -i $i ` echo $i | tr '[A-Z]' '[a-z]' |                       \
          perl -we '{ while(<>) { chomp; print "\x27$_\x27\n" } }'`;
    done;
    count=$[$count + 1];
done


                      __   _                 
Ben Phillips         /  '_' )         ,,,    "Bulls do not win bull fights;
pynk at angband.org     |  | ()|||||||||[:::)    people do.  People do not win
                     \__.-._)         '''     people fights; lawyers do."
                                                   -- Norman Augustine

--
To unsubscribe: mail majordomo at ale.org with "unsubscribe ale" in message body.





More information about the Ale mailing list