LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-23-2004, 09:15 AM   #1
drisay
Member
 
Registered: Sep 2004
Distribution: Slackware 10
Posts: 167

Rep: Reputation: 30
bash script for loop


Code:
  for i in $(ls -1Q somedirectory); do
    echo $i
  done
in the above example i get all separate words instead of an entire row. my problem is that i have directories that have spaces in them. without correcting the spaces in the directory names... how do i get an entire directory name????
 
Old 12-23-2004, 09:27 AM   #2
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
How about ls -d ?
 
Old 12-23-2004, 09:31 AM   #3
drisay
Member
 
Registered: Sep 2004
Distribution: Slackware 10
Posts: 167

Original Poster
Rep: Reputation: 30
the for loop still breaks it up into words
 
Old 12-23-2004, 12:13 PM   #4
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Code:
[/home/soumen/tmp/tmp] $ ls -l
total 4
-rw-rw-r--    1 soumen   soumen          0 Dec 23 23:38 a
-rw-rw-r--    1 soumen   soumen          0 Dec 23 23:38 abcd asdfsadf
-rw-rw-r--    1 soumen   soumen          0 Dec 23 23:38   as  asdf asdf 
-rw-rw-r--    1 soumen   soumen          0 Dec 23 23:38 b
-rwxrwxr-x    1 soumen   soumen         51 Dec 23 23:42 tt
[/home/soumen/tmp/tmp] $ cat tt
#!/bin/sh

for i in "$(ls)" ; do
        echo "${i}"
done
[/home/soumen/tmp/tmp] $ tt
a
abcd asdfsadf
  as  asdf asdf 
b
tt
[/home/soumen/tmp/tmp] $
HTH.
 
Old 12-24-2004, 04:36 PM   #5
C0NIk
LQ Newbie
 
Registered: Oct 2003
Posts: 25

Rep: Reputation: 15
try this url

http://jan.netcomp.monash.edu.au/ssw...tml#For%20loop
 
Old 12-25-2004, 12:32 AM   #6
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
for dir in "$somedirectory"/*; do
echo "`basename "$dir"`"
done

The quoting is important here.
 
  


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
bash script loop bong.mau Programming 6 09-14-2005 07:38 PM
Bash Script and Loop error handling Kedelfor Programming 5 05-22-2005 02:22 PM
Bash for loop Genjix Programming 5 12-23-2004 02:56 AM
bash script - for loop question rignes Programming 3 10-05-2004 11:16 PM
while loop ending prematurely in a bash script meat-head Programming 7 05-08-2004 01:46 AM


All times are GMT -5. The time now is 10:52 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration