LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 09:45 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 200

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, 10:14 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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, 10:23 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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, 02:28 PM   #4
DJ Shaji
Member
 
Registered: Dec 2004
Location: Yo Momma's house
Distribution: Fedora Rawhide, ArchLinux
Posts: 518
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, 01:21 AM   #5
unclesamcrazy
Member
 
Registered: May 2013
Posts: 200

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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:02 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
Open Source Consulting | Domain Registration