LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 02-24-2013, 06:30 PM   #16
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946

I'm usually a stickler for this kind of thing, but I actually think it doesn't matter too much to worry about filenames with newlines in them, particularly when working directly on the command line or in other quick&dirty situations.

That said, when writing a script that will be used repeatedly, it is generally better to do it correctly the first time. Then you'll never have to worry about correcting it later.

The main issue I have with propofol's script is here:

Code:
list=($(ls -1 *.mp3))
This is a completely useless use of ls, even considering that IFS was changed to avoid the parsing ls problem. All ls is doing here is acting as a glorified echo, since the shell is expanding the filenames into an argument list anyway before it even runs.

I will also say that changing IFS globally is generally bad form, and can lead to non-transparent code and hard-to-diagnose errors.

As usual, simple globbing is all you generally need, either to set an array, or processed directly in a for loop.

Code:
list=( *.mp3 )
The use of find is a bit more problematic, as it would depend on word splitting to work properly in the above. For that you should avoid command substitution entirely and use a while+read loop, with null separators.

Code:
while IFS='' read -r -d '' fname; do
    list+=( "$fname" )
done < <( find ~/mp3folder/ -iname '*.mp3' -print0 )

How can I find and deal with file names containing newlines, spaces or both?
http://mywiki.wooledge.org/BashFAQ/020


One more point that hasn't been mentioned yet. Never use #!/bin/sh as the shebang unless you specifically need POSIX-compatible portability. If you intend to rely on Bash specific features like arrays, then you need to call it explicitly with #!/bin/bash. Otherwise it won't work properly if the system you run it on doesn't have bash as its system shell.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
php scripts cant locate filenames with spaces latavish Programming 4 04-13-2011 02:55 PM
bash scripting: filenames with spaces joanq Programming 2 05-24-2005 12:50 PM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 07:10 AM
bash and filenames with spaces doctorwebbox Linux - General 2 12-15-2004 08:55 PM
bash, loops and spaces in filenames shy Programming 5 11-08-2004 07:43 AM


All times are GMT -5. The time now is 06:26 PM.

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