f



UNIX shell script question

I try with a UNIX shell script to do some actions in each subdirectories. 
Here is my script:

#!/bin/sh

DIRS=`ls -Q`

for userdir in $DIRS
do
       #do some action on each dir
        echo $userdir
done

My problem is that I have directories with whitespaces in their name. For 
example I have the following directories:

test 1
test 2
test 3
test 4

I get the following output:
"test
1"
"test
2"
"test
3"
"test
4"

I would expect the following:
"test 1"
"test 2"
"test 3"
"test 4"

In the variable DIRS I get the following value: "test 1" "test 2" "test 3" 
"test 4"
The problem is that in the list for the for-loop each token is separated by 
a whitespace. Although I execute 'ls -Q' to get double quotes around the 
names of directories, the for-loop still "sees" whitespaces in the double 
quoted string!
How can I fix this problem? Any help would be appreciated!

Thank you.

C�dric


0
11/14/2006 12:25:57 PM
comp.os.linux.misc 33599 articles. 1 followers. amosa69 (78) is leader. Post Follow

3 Replies
494 Views

Similar Articles

[PageSpeed] 40

C=E9dric Pillonel wrote:
> I try with a UNIX shell script to do some actions in each subdirectorie=
s.=20
> Here is my script:
>=20
> #!/bin/sh
>=20
> DIRS=3D`ls -Q`
>=20
> for userdir in $DIRS
> do
>        #do some action on each dir
>         echo $userdir
> done
>=20
> My problem is that I have directories with whitespaces in their name. F=
or=20
> example I have the following directories:
>=20
> test 1
> test 2
> test 3
> test 4
>=20
> I get the following output:
> "test
> 1"
> "test
> 2"
> "test
> 3"
> "test
> 4"
>=20
> I would expect the following:
> "test 1"
> "test 2"
> "test 3"
> "test 4"
>=20
> In the variable DIRS I get the following value: "test 1" "test 2" "test=
 3"=20
> "test 4"
> The problem is that in the list for the for-loop each token is separate=
d by=20
> a whitespace. Although I execute 'ls -Q' to get double quotes around th=
e=20
> names of directories, the for-loop still "sees" whitespaces in the doub=
le=20
> quoted string!
> How can I fix this problem? Any help would be appreciated!

Once the quotes are "inside" the variable, they are not treated as shell =

meta characters.

Better:
     for userdir in *
     do workon "$userdir"	# quotes necessary
        echo "$userdir"		# not strictly necessary
     done

Take a look at set -x to help in finding out.

--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett

0
11/14/2006 12:53:22 PM
"C�dric Pillonel" <cedric.pillonel@swisscom.com> writes:

> I try with a UNIX shell script to do some actions in each subdirectories. 
> Here is my script:
>
> #!/bin/sh
>
> DIRS=`ls -Q`
> for userdir in $DIRS
> do
>        #do some action on each dir
>         echo $userdir
> done

mkdir /tmp/ex ; cd /tmp/ex ; mkdir 'a b' c 'd e' f
DIRS=( * )
for userdir in "${DIRS[@]}" ; do
    echo $userdir
done

gives:

a b
c
d e
f

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

ADVISORY: There is an extremely small but nonzero chance that,
through a process known as "tunneling," this product may
spontaneously disappear from its present location and reappear at
any random place in the universe, including your neighbor's
domicile. The manufacturer will not be responsible for any damages
or inconveniences that may result.
0
pjb (7869)
11/14/2006 2:00:56 PM
On 2006-11-14, C�dric Pillonel wrote:
> I try with a UNIX shell script to do some actions in each subdirectories. 
> Here is my script:
>
> #!/bin/sh
>
> DIRS=`ls -Q`

   Don't use ls; it's the cause of your problem.

> for userdir in $DIRS

for userdir in */

> do
>        #do some action on each dir
>         echo $userdir

printf "%s\n" "$userdir"

> done
>
> My problem is that I have directories with whitespaces in their name. For 
> example I have the following directories:
>
> test 1
> test 2
> test 3
> test 4
>
> I get the following output:
> "test
> 1"
> "test
> 2"
> "test
> 3"
> "test
> 4"

   As you should.

> I would expect the following:
> "test 1"
> "test 2"
> "test 3"
> "test 4"

   Word splitting does not know about quotes.

> In the variable DIRS I get the following value: "test 1" "test 2" "test 3" 
> "test 4"
>
> The problem is that in the list for the for-loop each token is separated by 
> a whitespace. Although I execute 'ls -Q' to get double quotes around the 
> names of directories, the for-loop still "sees" whitespaces in the double 
> quoted string!
> How can I fix this problem? Any help would be appreciated!

   Don't use ls. Quote your variables.

-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |         is released under the
   2005, Apress                 |    GNU General Public Licence
0
cfajohnson (1827)
11/14/2006 8:26:34 PM
Reply: