LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 02-08-2011, 05:13 PM   #1
jsteel
Member
 
Registered: Mar 2007
Location: England
Distribution: Arch
Posts: 392

Rep: Reputation: 34
Script to pass multiple files to program


I'm playing around with a bash script to pass files to a program (such as VLC). I thought something like this would work:

vlc $( ls | sed 's/\ /\\ /g' | tr '\n' ' ' )

ls shows the files in the current directory, then sed changes spaces to "\ " (to escape them) and finally tr removes the line breaks. I end up changing:

01 - Music Track.mp3
02 - Another Track.mp3

to

01\ -\ Music\ Track.mp3 02\ -\ Another\ Track.mp3

"vlc 01\ -\ Music\ Track.mp3 02\ -\ Another\ Track.mp3" works if I type it manually but my script reports a problem with `-\'

It seems it's not escaping the spaces.

Any help is much appreciated. Thanks.
 
Old 02-08-2011, 05:41 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

I am not sure why this is not working. If you prepend your command with an echo the output looks ok. If you copy+paste the echoed line into the terminal again and hit enter then it executes just fine. It just does not do so when you try it as you have done. Maybe I will investigate this later. Anyway, if the '-' give you trouble then this might be because it is misinterpreted as an option. To bypass the funny command substitution issue this:
Code:
vlc -- *
Since you want to play all files in that directory. The '--' is to tell vlc that there are no more options to parse.
 
Old 02-08-2011, 07:27 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
I am not sure why this is not working. If you prepend your command with an echo the output looks ok.
Testing with echo isn't good enough, it doesn't show you how the arguments are parsed:

Code:
~/tmp/mp3$ ls
01 - Music Track.mp3  02 - Another Track.mp3
~/tmp/mp3$ echo vlc $( ls | sed 's/\ /\\ /g' | tr '\n' ' ' )
vlc 01\ -\ Music\ Track.mp3 02\ -\ Another\ Track.mp3
~/tmp/mp3$ args.sh vlc $( ls | sed 's/\ /\\ /g' | tr '\n' ' ' )
arg1: vlc
arg2: 01\
arg3: -\
arg4: Music\
arg5: Track.mp3
arg6: 02\
arg7: -\
arg8: Another\
arg9: Track.mp3
~/tmp/mp3$ args.sh vlc *
arg1: vlc
arg2: 01 - Music Track.mp3
arg3: 02 - Another Track.mp3
args.sh:
Code:
#!/bin/sh
i=1
while [ -n "$1" ] ; do
    echo arg$i: "$1"
    shift
    i=`expr $i + 1`
done
 
Old 02-08-2011, 08:41 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Yes, I had noticed that the output gets split up that way. You do not really need an extra script for that. I got the corresponding 'No such file or directory' error messages in vlc.

The interesting thing at this point, however, is why are the parameters handled this way? Is vlc responsible for splitting them up or is it bash? If it were vlc then this would happen with every filename that has a space in it, thus making it impossible for vlc to play files with spaces in them. Which leaves bash as the last suspect. My guess at this point is that after a command substitution bash does not parse the result. Apparently it simply splits up the resulting string at every space and feeds the tokens to vlc one by one - with the escape character '\'.
However, this behavior does not seem very conclusive at this point. Now it would be understandable that after a command substitution the result is untouched and passed as is to vlc. But this is not the case. It does process it to some degree, i.e. splitting it up. If it does some post-processing then why not do the whole thing and recognizing the escape sequences? Are there any good reasons not to? The way it deals with it seems like going only half-way. Is there a "switch" that can trigger an alternate behavior?

That being said I am open to explanations/corrections and/or good reasons why it has to be this way and not otherwise.
 
Old 02-08-2011, 09:04 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
The interesting thing at this point, however, is why are the parameters handled this way? Is vlc responsible for splitting them up or is it bash?
vlc was never called in my example.


Quote:
It does process it to some degree, i.e. splitting it up. If it does some post-processing then why not do the whole thing and recognizing the escape sequences? Are there any good reasons not to?
Quote:
The order of expansions is: brace expansion, tilde expansion, parameter, variable, and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and filename expansion.

Shell Expansions

After the preceding expansions, all unquoted occurrences of the characters ‘\’, ‘'’, and ‘"’ that did not result from one of the above expansions are removed.
Quote Removal
That is the current documented behaviour of bash. As for a "good" reason: I don't think bash (or sh, csh) is well designed, it seems like it just accumulates features as time goes on. It has to be this way because changing would break too many scripts.
 
  


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
[SOLVED] Make script that runs program as another user with pass? LuxLuv Programming 4 06-08-2010 10:34 AM
How to search for missing files and pass their names on to another shell script djslothario Linux - Newbie 3 08-07-2009 12:59 AM
PHP script to retrieve records from MYSQL db and pass them to an external program Julianus Programming 3 08-03-2006 02:09 PM
How to pass data to a console program with multiple inputs? juanbobo Programming 3 08-18-2005 06:39 PM
script (or other) to pass variables to C++ program ngwenyama Programming 9 01-20-2005 09:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:01 AM.

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