You are not logged in.

#1 2010-08-22 20:28:57

pokraka
Member
From: Brussels
Registered: 2009-01-15
Posts: 63

Bourne shell, blank characters and quotations

Hello.

I can't figure out how to use single and double quotations in Bourne shell.

Here's a part of a script I'm writing to create a matroska video with several different subtitles :

title=`echo "$1" | sed 's/\.[^.]*$//'`

subs=''
for srt in "$title".*.srt; do
    slang=`echo "$srt" | sed 's/.*\.\(...\)\.srt/\1/'`
    subs="${subs} --language 0:$slang '$srt'"
done

mkvmerge -o "$title.mkv" $subs -S "$1"

When I type ./myscript 'Gadjo Dilo.avi', it should execute this command :

mkvmerge -o 'Gadjo Dilo.mkv' --language 0:eng 'Gadjo Dilo.eng.srt' --language 0:fre 'Gadjo Dilo.fre.srt' -S 'Gadjo Dilo.avi'

But, using set -x, I discovered it actually execute this :

mkvmerge -o 'Gadjo Dilo.mkv' --language 0:eng ''\''Gadjo' 'Dilo.eng.srt'\''' --language 0:fre ''\''Gadjo' 'Dilo.fre.srt'\''' -S 'Gadjo Dilo.avi'

How should I fix this ? I tried without success many different combinations of single and double quotes.

Thanks.

Last edited by pokraka (2010-08-22 20:29:35)

Offline

#2 2010-08-22 20:46:10

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bourne shell, blank characters and quotations

Remove spaces: Gadjo Dilo.mkv -> Gadjo_Dilo.mkv.
I think that's not what you're asking about, but it should help too, maybe even better :-)

Offline

#3 2010-08-23 17:06:39

pokraka
Member
From: Brussels
Registered: 2009-01-15
Posts: 63

Re: Bourne shell, blank characters and quotations

Yes of course I know of this trick but I can't rename all my files.
Any other suggestion ?

Offline

#4 2010-08-23 17:35:12

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bourne shell, blank characters and quotations

pokraka wrote:

Yes of course I know of this trick but I can't rename all my files.
Any other suggestion ?

Why can't you rename your files?

Instead of

mkvmerge -o "$title.mkv" $subs -S "$1"

try

mkvmerge -o "$title.mkv" "$subs" -S "$1"

(quotes around $subs added)
It helps a bit, but I honestly can't read your code - are all these filename mangling transformations really necessary?

Edit: I'm not using bourne shell, but neither bash nor dash wanted to cooperate fully.

subs="${subs} --language 0:$slang $srt"

(quotes around $srt removed)

Last edited by karol (2010-08-23 17:59:29)

Offline

#5 2010-08-23 18:04:58

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bourne shell, blank characters and quotations

Quick solution: use Bash instead of Bourne.

#!/bin/bash

title=${1%.*}

subs=()
for srt in "$title".*.srt; do
  slang=${srt%.*}; slang=${slang##*.}
  subs+=("--language" "0:$slang" "$srt")
done

mkvmerge -o "$title.mkv" "${subs[@]}" -S "$1"

I tested a little bit, but no refunds if your dog gets kicked.

Last edited by falconindy (2010-08-23 18:22:50)

Offline

#6 2010-08-23 18:07:12

frabjous
Member
Registered: 2010-07-13
Posts: 367

Re: Bourne shell, blank characters and quotations

What happens if you replace all of lines 3-8 with something simpler, like:

subs=$(ls | sed -n "s/$title\.\(...\)\.srt/--language 0:\1 '&'/p" | tr '\n' ' ')

Offline

#7 2010-08-23 18:07:45

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bourne shell, blank characters and quotations

falconindy wrote:

Quick solution: use Bash instead of Bourne.

#!/bin/bash

title=${1%.*}

subs=()
for srt in "$title".*.srt; do
  slang=${srt%.*}; slang=${slang##*.}
  subs+=("--language" "0:$slang" "$srt")
done

echo mkvmerge -o "$title.mkv" "${subs[@]}" -S "$1"

I tested a little bit, but no refunds if your dog gets kicked.

Does it work for filenames with spaces in them?

Offline

#8 2010-08-23 18:11:21

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bourne shell, blank characters and quotations

edit: dbl post

Last edited by falconindy (2010-08-23 18:13:40)

Offline

#9 2010-08-23 18:12:11

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bourne shell, blank characters and quotations

karol wrote:

Does it work for filenames with spaces in them?

There's a reason I opted to use an array. Please take a visit to the Wooledge BashFAQ if you'd like to learn more about quoting in Bash.

frabjous wrote:

What happens if you replace all of lines 3-8 with something simpler, like:

subs=$(ls | sed -n "s/$title\.\(...\)\.srt/--language 0:\1 '&'/p" | tr '\n' ' ')

You call this simpler? Not only is it even more confusing, you're succumbing to a major bash pitfall. Don't ever try to parse the output of ls.

Last edited by falconindy (2010-08-23 18:13:25)

Offline

#10 2010-08-23 18:15:55

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bourne shell, blank characters and quotations

@ falconindy

mkvmerge -o Gadjo Dilo.mkv --language 0:eng Gadjo Dilo.eng.srt --language 0:fre Gadjo Dilo.fre.srt -S Gadjo Dilo.avi

I think you need the single quotes: 'Gadjo Dilo.eng.srt'.

Edit: if you remove the echo part it will work :-)

Edit 2: One-liners are simpler by virtue of having less characters.

Last edited by karol (2010-08-23 18:18:46)

Offline

#11 2010-08-23 18:21:51

frabjous
Member
Registered: 2010-07-13
Posts: 367

Re: Bourne shell, blank characters and quotations

You call this [em]simpler[/em]?

I just meant shorter.

Don't [em]ever[/em] try to parse the output of ls.

Hey, I'll do what I want, thank you very much. (That said, I do know this is risky business, but I thought it might actually help in this case.)

Offline

#12 2010-08-23 18:21:55

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Bourne shell, blank characters and quotations

Right. Typo on my part. I'll just leave these here...

http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/ParsingLs

Last edited by falconindy (2010-08-23 18:23:41)

Offline

Board footer

Powered by FluxBB