LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-06-2014, 10:45 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Rep: Reputation: 1
for loop question


I have a directory ~/test in which several sub-directories are there.
The sub-directory name has spaces and I can not rename them because the name depends on several processes.
The names are like
Code:
 a 1
b 3 2
c  
 d 
 f   9
There is a for loop in a shell script, which is not running successfully because of these spaces.
Code:
#!/bin/bash
lst=$(ls ~/test)
for i in $lst; do echo "$i"; done
it prints the results
Code:
a
1
b
3
2
c
d
f
9
It is not correct, I want to use commands in "do" section of loop which needs correct directory name, now a,1,3,2... does not exist that's why the loop is giving error.
Please help
 
Old 05-06-2014, 11:14 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,110

Rep: Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527
Just change around the field seperator.

Script:
Code:
#!/bin/bash
OLD=$IFS
IFS="\n"

lst=$(ls /home)
for i in $lst; do echo "$i"; done

IFS=$OLD
Output:
Code:
drwxr-xr-x 2 root root 4096 May  6 11:11 a 11
drwxr-xr-x 2 root root 4096 May  6 11:11 a b
drwxr-xr-x 2 root root 4096 May  6 11:11 b 1 a
[root@dev home]# bash run.sh
a 11
a b
b 1 a
 
1 members found this post helpful.
Old 05-06-2014, 11:23 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,358

Rep: Reputation: 1990Reputation: 1990Reputation: 1990Reputation: 1990Reputation: 1990Reputation: 1990Reputation: 1990Reputation: 1990Reputation: 1990Reputation: 1990Reputation: 1990
Use globbing directly in your for loop, don't use an intermediate variable
Code:
for i in ~/test/*; do echo $i; done
When you use an intermediate variable, the for loop splits using IFS. When you use the globbing directly in the for loop it doesn't.
 
1 members found this post helpful.
Old 05-06-2014, 03:28 PM   #4
DJ Shaji
Member
 
Registered: Dec 2004
Location: Yo Momma's house
Distribution: Fedora Rawhide, ArchLinux
Posts: 513
Blog Entries: 15

Rep: Reputation: 106Reputation: 106
Thumbs up 2 people like this

Quote:
Originally Posted by suicidaleggroll View Post
Code:
for i in ~/test/*; do echo $i; done
You might want to put $i in quotes, otherwise anything except echo won't find the directories:

Code:
for i in ~/test/*; do echo "$i"; done
 
Old 05-07-2014, 02:21 AM   #5
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
Thanks to all for your responses.

Thanks suicidaleggroll for this wonderful one line solution.

Thanks szboardstretcher, it worked for me. I could do another task because of you.
I had a list of databases which appeared when I ran ls -1, I wanted to take backup of each database but same problem was there. There were spaces in database's name. I was using mysqldump after 'do' statement but this IFS="\n" was giving little trouble, it was changing the line in the list when letter 'n' was appeared in database name so I used IFS=$'\n' and it worked for me.
Thanks again !!!
 
  


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
question in a for loop john83reuben Programming 7 10-08-2013 11:59 PM
[SOLVED] Libipq and C question (for loop) MiniComa Programming 7 07-07-2012 08:53 AM
for loop question s_linux Programming 4 09-07-2011 07:26 PM
while loop question IceOner Programming 6 10-26-2007 09:23 AM
BASH 'while' loop question GSMD Programming 4 04-13-2007 01:51 AM


All times are GMT -5. The time now is 03:01 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