niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
Spaces in directory names & script
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Poochieİ
*nix forums beginner


Joined: 08 Jul 2005
Posts: 10

PostPosted: Wed Feb 09, 2005 1:28 pm    Post subject: Re: Spaces in directory names & script Reply with quote

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:

[CUT]

Quote:
Quote the dirnames:

[CUT]

Thanks to both. It actually works! My problem was with the TextEditor I
was using (TextWrangler under OSX) that printed some weird stylish
quotes instead of the default straight ones.

--
Poochieİ
Back to top
Rakesh Sharma
*nix forums beginner


Joined: 28 Feb 2005
Posts: 36

PostPosted: Wed Feb 09, 2005 6:52 am    Post subject: Re: Spaces in directory names & script Reply with quote

Poochieİ wrote:
Quote:

I got this codetaken from a script of mine:

for dirname in *;
do
if [ -d $dirname ]
then
echo entrando in: $dirname
cd $dirname

[CUT]

what must I do to make it handle directories with whitespaces in
their
name?



## Notes:i) prefix the wildcards with a ./ so that any funny
## characters(e.g., - ! etc.) are shielded from the commands
## where they are going to be used

## instead of 'echo' use either 'printf' or the << operator

## the wildcards are shown here to look for ``hidden'' directories
## also.

## use quotes around your variables to stop word splitting, IOW
## preserve spaces/TABS/newlines in your dir. names

## used 'd_name' as a variable name since 'dirname' is a command
## (although it's not a pbm. as such)



for d_name in ./* ./.[!.]* ./..?*;do

[ -d "${d_name}" ] || continue

cat - << [EOF]
entrando in: '${d_name}'
[EOF]

cd "${d_name}"

done

Or this:

for d_name in ./* ./.*;do

[ -d "${d_name}" ] || continue

case ${d_name} in './.'|'./..') continue;; esac

printf 'entrando in: "%s"\n' "${d_name}"
cd "${d_name}"
done
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Wed Feb 09, 2005 5:44 am    Post subject: Re: Spaces in directory names & script Reply with quote

On Wed, 09 Feb 2005 at 06:20 GMT, Poochieİ wrote:
Quote:
Hi all,

I got this codetaken from a script of mine:

for dirname in *;
do
if [ -d $dirname ]
then
echo entrando in: $dirname
cd $dirname

[CUT]

what must I do to make it handle directories with whitespaces in their
name?

Quote the dirnames:

for dirname in *;
do
if [ -d "$dirname" ]
then
echo entrando in: "$dirname"
cd "$dirname"

I'd rewrite the last two line to test for success of cd:

cd "$dirname" && echo entrando in: "$dirname" || : Error action here


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
===================================================================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Back to top
Poochieİ
*nix forums beginner


Joined: 08 Jul 2005
Posts: 10

PostPosted: Wed Feb 09, 2005 5:20 am    Post subject: Spaces in directory names & script Reply with quote

Hi all,

I got this codetaken from a script of mine:

for dirname in *;
do
if [ -d $dirname ]
then
echo entrando in: $dirname
cd $dirname

[CUT]

what must I do to make it handle directories with whitespaces in their
name?

Very thanks in advance,
--
Poochieİ
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 10:49 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts postfix - mail directory bandito Postfix 0 Tue Dec 09, 2008 11:13 pm
No new posts does squid 2.6 support setting cache_peer port in redirec... Victor Tsang Squid 0 Fri Jul 21, 2006 8:16 am
No new posts mail script eeb4u@hotmail.com shell 3 Fri Jul 21, 2006 5:50 am
No new posts change default install directory when using bdist_rpm krithika.sridhar@gmail.co python 1 Fri Jul 21, 2006 4:09 am
No new posts A simple bash script JPB Suse 2 Fri Jul 21, 2006 2:19 am

Satellite TV | Loans | Mobile Phone | Remortgaging | MPAA
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1938s ][ Queries: 20 (0.1110s) ][ GZIP on - Debug on ]