LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-15-2009, 04:20 AM   #1
klss
LQ Newbie
 
Registered: Oct 2008
Distribution: Mint
Posts: 22

Rep: Reputation: 15
Scripting & Arrays : How to fill an array with filenames which consist of spaces?


Hi folks.

I have a problem with spaces-in-filenames and array fields.

I can't get the fields properly defined if I e.g. do this:

Code:
#!/bin/bash

directory=$1

arraytmp=/dev/shm/arraytmp

ls -1 "$directory"| while read K ; do 
echo "$K" >> $arraytmp 
done

files=( $(cat "$arraytmp") )

cnt=${#files[@]}

for (( I = 0 ; I < cnt ; I++ )) ; do
echo ${files[${I}]}
done
What am I doing wrong here?

THX again

Last edited by klss; 01-15-2009 at 04:22 AM.
 
Old 01-15-2009, 05:23 AM   #2
zer0x333
Member
 
Registered: Oct 2007
Posts: 31

Rep: Reputation: 16
IFS

Hi,

I would change the bash IFS (Internal Field Seperator - man bash). I am probably doing it totally wrong here, but it works!

Can anyone can tell me the best way to specify newline here?

Code:
#!/bin/bash
IFS="

"
<rest of script>
Cheers,
zer0x
 
Old 01-15-2009, 12:05 PM   #3
klss
LQ Newbie
 
Registered: Oct 2008
Distribution: Mint
Posts: 22

Original Poster
Rep: Reputation: 15
In fact -- below seems to work. THX a lot. I need to check it out a bit further.

Quote:

OLDIFS=$IFS
IFS=$'\n'
files=( $(ls -1 "$file") )
IFS=$OLDIFS


Last edited by klss; 01-15-2009 at 12:48 PM.
 
Old 01-18-2009, 09:51 AM   #4
klss
LQ Newbie
 
Registered: Oct 2008
Distribution: Mint
Posts: 22

Original Poster
Rep: Reputation: 15
Hi folks.

I need to extent "spaces" to other weired characters.
Something like '`!:" can also cause problems. Quite obvious - isn't it.
I guess you guys know the cddb filenames!

Any ideas?

Below is another interesting solution, I found, how to handle spaces without fiddling around with IFS. I am wondering if I should try to
apply this. Are there similar expression such as [[:space:]]
for the other weired characters?

THX again.

Code:
list=$(ls -1 | sed -e s/[[:space:]]/SP/g )
for file in $list
do
file=$(echo $file | sed -e s/SP/\ /g)
echo $file
done
 
Old 01-18-2009, 11:35 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
http://www.mkssoftware.com/docs/man5/regexp.5.asp
 
Old 01-19-2009, 12:16 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Post #5 won't work because you are adding the spaces back too soon. You could read in the array with the "SP" characters, and then iterate through the array converting SP to spaces.

You could substitute a non-printing character instead. In the shell you can insert control characters by pressing [CTRL-v] just before it. Just don't use one that would cause a problem like CTRL-D which will exit the shell.
 
  


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
spaces in filenames dotancohen Programming 12 02-17-2006 02:42 PM
filenames with spaces antony.booth Programming 5 11-01-2005 04:49 AM
bash scripting: filenames with spaces joanq Programming 2 05-24-2005 12:50 PM
Scripting question reg. filenames with spaces Awfki Linux - Newbie 3 05-16-2004 08:01 PM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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