Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
UNIX shell script question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Cédric Pillonel  
View profile  
 More options Nov 14 2006, 11:25 pm
Newsgroups: alt.comp.linux, alt.comp.os.linux, comp.os.linux.development.system, comp.os.linux.misc
From: "Cédric Pillonel" <cedric.pillo...@swisscom.com>
Date: Tue, 14 Nov 2006 13:25:57 +0100
Local: Tues, Nov 14 2006 11:25 pm
Subject: 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josef Moellers  
View profile  
 More options Nov 14 2006, 11:53 pm
Newsgroups: alt.comp.linux, alt.comp.os.linux, comp.os.linux.development.system, comp.os.linux.misc
From: Josef Moellers <josef.moell...@fujitsu-siemens.com>
Date: Tue, 14 Nov 2006 13:53:22 +0100
Local: Tues, Nov 14 2006 11:53 pm
Subject: Re: UNIX shell script question

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.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Bourguignon  
View profile  
 More options Nov 15 2006, 1:00 am
Newsgroups: alt.comp.linux, alt.comp.os.linux, comp.os.linux.development.system, comp.os.linux.misc
From: Pascal Bourguignon <p...@informatimago.com>
Date: Tue, 14 Nov 2006 15:00:56 +0100
Local: Wed, Nov 15 2006 1:00 am
Subject: Re: UNIX shell script question

"Cédric Pillonel" <cedric.pillo...@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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris F.A. Johnson  
View profile  
 More options Nov 15 2006, 7:26 am
Newsgroups: alt.comp.linux, alt.comp.os.linux, comp.os.linux.development.system, comp.os.linux.misc
From: "Chris F.A. Johnson" <cfajohn...@gmail.com>
Date: Tue, 14 Nov 2006 15:26:34 -0500
Local: Wed, Nov 15 2006 7:26 am
Subject: Re: UNIX shell script question
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"

   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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google