You are not logged in.

#1 2009-05-06 10:10:47

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

strange script problem... (spaces in filenames)

i chose mplayer as example here... when i try to play a random file with the command movie=`ls |sort --random-sort | head -1` && mplayer $movie i get this:

carnager@freebox /mnt/media/_movies % movie=`ls |sort --random-sort | head -1` && mplayer $movie
MPlayer dev-SVN-rUNKNOWN-4.3.3 (C) 2000-2009 MPlayer Team
CPU: AMD Athlon(tm) 64 X2 Dual Core Processor 3800+ (Family: 15, Model: 43, Stepping: 1)
CPUflags:  MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 1
Compiled with runtime CPU detection.
137 audio & 292 video codecs
Cannot load bitmap font: /home/carnager/.mplayer/subfont.ttf
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
 
Playing The\ Place\ Promised\ in\ Our\ Early\ Days.avi.
File not found: 'The\ Place\ Promised\ in\ Our\ Early\ Days.avi'
Failed to open The\ Place\ Promised\ in\ Our\ Early\ Days.avi.

as you can see, the filename has been escaped correctly.. lets copy and paste it for the regular mplayer command:

carnager@freebox /mnt/media/_movies % mplayer The\ Place\ Promised\ in\ Our\ Early\ Days.avi 
MPlayer dev-SVN-rUNKNOWN-4.3.3 (C) 2000-2009 MPlayer Team
CPU: AMD Athlon(tm) 64 X2 Dual Core Processor 3800+ (Family: 15, Model: 43, Stepping: 1)
CPUflags:  MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 1
Compiled with runtime CPU detection.
137 audio & 292 video codecs
Cannot load bitmap font: /home/carnager/.mplayer/subfont.ttf
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
 
Playing The Place Promised in Our Early Days.avi.
AVI file format detected.

Suddenly it works... Can anyone explain this to me?
I know it does work if i put $movie in " ", but i just dont get it...

Last edited by Rasi (2009-05-06 10:26:33)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#2 2009-05-06 10:58:21

Wra!th
Member
Registered: 2009-03-31
Posts: 342

Re: strange script problem... (spaces in filenames)

Just go with

mplayer "$(ls | sort --random-sort | head -1)"

or something similar

Last edited by Wra!th (2009-05-06 10:59:10)


MacGregor DESPITE THEM!
7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

Offline

#3 2009-05-06 11:33:21

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: strange script problem... (spaces in filenames)

well yes i know this works... my question is: since the filename was escaped correctly in the original command, why doesnt it work? i am not looking for alternative solutions, just for enlightenment smile


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#4 2009-05-06 12:21:47

kumyco
Member
From: somewhere
Registered: 2008-06-23
Posts: 153
Website

Re: strange script problem... (spaces in filenames)

they got passed with the backslashes in the filename, not unescaped as was intended -- if you look at mplayer output, the filenames are different

Offline

#5 2009-05-06 13:12:43

carstenson
Member
Registered: 2009-04-22
Posts: 2

Re: strange script problem... (spaces in filenames)

Looks like the error output is escaping the spaces, but that does not mean they were escaped going in. Your ls command is going to return the standard file name, without any escaped characters. You are then intending this to be fed as arg1 to mplayer. There is probably a way of escaping other codes, but if you want spaces included in any argument, you should quote the argument (i.e. mplayer "$movie"). Spaces in filenames always cause this kind of problem in shell scripts. I try to avoid them when I can.

Offline

#6 2009-05-06 13:40:51

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: strange script problem... (spaces in filenames)

You should mention you use zsh.

In bash when you try the first mplayer will try to open 7 files.

It looks like zsh is doing this:
ls (The Place...) | zsh-escaping (The\ Place...) | sort -R | zsh-escaping (The\\\ Place...) | head -n 1
&&
mplayer The\\\ Place...     (which quoted is: mplayer "The\ Place...")

Offline

Board footer

Powered by FluxBB