LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-30-2009, 09:26 AM   #1
dzcharles
LQ Newbie
 
Registered: May 2009
Location: Belgium
Distribution: Debian 5.0
Posts: 14

Rep: Reputation: 0
Problem with ls in a bash script


Hi

the thing I want to do is saving the output in an array/for loop in a bash script, so I can use them later on...

The problem is that the names of the directories contains multiple words,
example: "accounting January 2010"

I've tried the following:

Code:
for i in $(ls /home/karel/accounting)
do
echo "$i"
done
i get the following output:
accounting
January
2009

as you can see every word of the directory is an entry
instead of "accounting January 2009" i get "accounting", "January" and "2009", 3 separate words

now this is the problem
someone knows a solution? so that every time the $i is called, the name of a directory is shown and not just one word...

Thx and happy new-year to you all

Dzcharles
 
Old 12-30-2009, 09:37 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
The general answer is: Don't use filenames with spaces.

This illustrates one way around the problem: (I created three files in a directory "names")

Code:
[mherring@Ath names]$ ls -l
total 0
-rw-r--r-- 1 mherring users 0 2009-12-30 06:41 one
-rw-r--r-- 1 mherring users 0 2009-12-30 06:41 third one
-rw-r--r-- 1 mherring users 0 2009-12-30 06:41 two
[mherring@Ath names]$ for fil in *; do echo $fil; done
one
third one
two
[mherring@Ath names]$ for fil in $(ls); do echo $fil; done
one
third
one
two
[mherring@Ath names]$ cd ..
[mherring@Ath play]$ for fil in names/*; do echo $fil; done
names/one
names/third one
names/two
[mherring@Ath play]$
 
Old 12-30-2009, 09:42 AM   #3
SethsdadtheLinuxer
Member
 
Registered: Jun 2007
Posts: 152

Rep: Reputation: 37
quick and dirty
cd /home/karel/accounting
for i in *
do
echo "$i"
done
 
Old 12-30-2009, 09:55 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,028

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Quote:
Originally Posted by pixellany View Post
The general answer is: Don't use filenames with spaces.
...
+ 1
 
Old 12-30-2009, 10:00 AM   #5
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
spaces in filenames are fine as long as you let the shell do the globbing, like Sethsdads example.

if its inconvenient to change cwd for some reason, you can also say:

for i in foo/bar/baz/*; do echo `basename $i`; done

often i'll assign the result of basename to a variable and reuse it later.
 
Old 12-30-2009, 10:03 AM   #6
dzcharles
LQ Newbie
 
Registered: May 2009
Location: Belgium
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you very much
this solves the problem I had
 
Old 12-30-2009, 10:46 AM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
Another solution is to change the field separator temporarily:
Code:
IFS='
' for i in $(ls /home/karel/accounting)
 
Old 12-31-2009, 03:33 AM   #8
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 675

Rep: Reputation: 136Reputation: 136
What about simple
Code:
ls -1 /home/karel/accounting
?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 11:53 AM
Problem with a bash script sloteel Programming 4 02-09-2009 03:12 PM
Problem with bash script. cheater1034 Programming 9 11-21-2005 11:29 PM
bash script problem fatbastard spice Linux - General 2 08-24-2005 03:15 AM
Problem with this bash script cantabile Programming 3 06-06-2005 06:31 PM


All times are GMT -5. The time now is 12:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration