LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash script for loop (http://www.linuxquestions.org/questions/programming-9/bash-script-for-loop-269566/)

drisay 12-23-2004 09:15 AM

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????

nixcraft 12-23-2004 09:27 AM

How about ls -d ?

drisay 12-23-2004 09:31 AM

the for loop still breaks it up into words

dustu76 12-23-2004 12:13 PM

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.

C0NIk 12-24-2004 04:36 PM

try this url

http://jan.netcomp.monash.edu.au/ssw...tml#For%20loop

randyding 12-25-2004 12:32 AM

for dir in "$somedirectory"/*; do
echo "`basename "$dir"`"
done

The quoting is important here.


All times are GMT -5. The time now is 11:33 PM.