ridgerunner7
September 14th, 2006, 10:52 PM
It's possible to use a quoted elements list in a for loop.
for i in "A blah" "B blah"; do
echo $i
done
which echo this:
A blah
B blah
What I want to do is obtain a directory list so that I can do actions on each directory. Sounds simple but the problem is some directories contain spaces. But using the quoted elements as above it shouldn't be a problem, right?
I'm using this because 'ls -d' doesn't seem to work correctly:
ls -pAQ | grep / | sed 's/\///'
So I'm trying something like this:
for i in $( ls -pAQ | grep / | sed 's/\///'); do
echo $i
done
which echo this:
"A
blah"
"B
blah"
OR to make it more obvious, using a variable, something like this:
DirectoryList=$( ls -pQ | grep / | sed 's/\///')
echo DirList=$DirectoryList; echo
for i in $DirectoryList; do
echo $i
done
which echo this:
DirList="A blah" "B blah"
"A
blah"
"B
blah"
Anybody got an explanation for why these don't work like the first instance? Is it a bug or am I misunderstanding some syntax here? Does anyone have a solution or workaround?
for i in "A blah" "B blah"; do
echo $i
done
which echo this:
A blah
B blah
What I want to do is obtain a directory list so that I can do actions on each directory. Sounds simple but the problem is some directories contain spaces. But using the quoted elements as above it shouldn't be a problem, right?
I'm using this because 'ls -d' doesn't seem to work correctly:
ls -pAQ | grep / | sed 's/\///'
So I'm trying something like this:
for i in $( ls -pAQ | grep / | sed 's/\///'); do
echo $i
done
which echo this:
"A
blah"
"B
blah"
OR to make it more obvious, using a variable, something like this:
DirectoryList=$( ls -pQ | grep / | sed 's/\///')
echo DirList=$DirectoryList; echo
for i in $DirectoryList; do
echo $i
done
which echo this:
DirList="A blah" "B blah"
"A
blah"
"B
blah"
Anybody got an explanation for why these don't work like the first instance? Is it a bug or am I misunderstanding some syntax here? Does anyone have a solution or workaround?