You are not logged in.

#1 2011-04-16 11:29:28

Awebb
Member
Registered: 2010-05-06
Posts: 6,683

[SOLVED][BASH] strange behavior of quote signs in variables

Abstract:

I have a folder full of ogm and srt files and I'm trying to mux them to mkv files. I usually would type:

mkvmerge -o file.mkv file.ogm file.srt

So far so good. Those files have numbers and spaces, they look like "01 - Title.ogm" So I would type:

mkvmerge - o "01 - Title.mkv" "01 - Title.ogm" "01 - Title.srt"

I learned that mkvmerge does not always like spaces after it's "-o", so I do:

mkvmerge - o 01_-_Title.mkv "01 - Title.ogm" "01 - Title.srt"

.

Those lines work well. Now I have the following script:

# !/bin/bash

for file in *.ogm
    do
        filepro=$(echo \"$file\")
        mkvfile=$(echo $filepro | sed s/ogm/mkv/ | sed s/[[:blank:]]/_/g)
        srtfile=$(echo $filepro | sed s/ogm/srt/)
        commandline="-o $mkvfile $filepro $srtfile"
        echo "Processing $file with $srtfile and output to $mkvfile"
        mkvmerge -o $mkvfile "$filepro" "$srtfile"
    done

It does not work, it complains about the fact that the file 01 can't be processed. Output is in german, but you should understand.

mkvmerge v4.6.0 ('Still Crazy After All These Years') gebaut am Mar 10 2011 23:46:43
Fehler: Die Quelldatei '01' konnte nicht geöffnet werden, oder aber die Größenabfrage schlug fehl.

Now the line "echo mkvmerge -o $mkvfile $filepro $srtfile" outputs a perfectly healthy line

mkvmerge - o 01_-_Title.mkv "01 - Title.ogm" "01 - Title.srt"

I solved the problem, but I still don't understand why it came up after all. I did the following: Instead of embedding the quote signs into the variable, I simply added them to the commandline. It now looks like this:

# !/bin/bash

for ogmfile in *.ogm
    do
        mkvfile=$(echo $ogmfile | sed s/ogm/mkv/)
        srtfile=$(echo $ogmfile | sed s/ogm/srt/)
        echo "Processing $file with $srtfile and output to $mkvfile"
        mkvmerge -o "$mkvfile" "$ogmfile" "$srtfile"
    done

While "echo mkvmerge -o $mkvfile "$filepro" "$srtfile" outputs a line without quote signs at all (like it looked when I completely ignored the blanks) the script runs as it should.

Now: Why can't I store a quote mark inside a variable to create a command line for further re-use like this:

commandline="-o $mkvfile $filepro $srtfile"

Where do I think the wrong way? Am I supposed to escape something? I experimented with escaping the quote sings on different positions, but it didn't work. Any hints on why it is the way it is?

Last edited by Awebb (2011-04-16 12:40:05)

Offline

#2 2011-04-16 11:58:28

Damnshock
Member
From: Barcelona
Registered: 2006-09-13
Posts: 414

Re: [SOLVED][BASH] strange behavior of quote signs in variables

Awebb wrote:
# !/bin/bash

for file in *.ogm
    do
        filepro=$(echo \"$file\")
        mkvfile=$(echo $filepro | sed s/ogm/mkv/ | sed s/[[:blank:]]/_/g)
        srtfile=$(echo $filepro | sed s/ogm/srt/)
        commandline="-o $mkvfile $filepro $srtfile"
        echo "Processing $file with $srtfile and output to $mkvfile"
        echo mkvmerge -o $mkvfile "$filepro" "$srtfile"
    done

It does not work, it complains about the fact that the file 01 can't be processed. Output is in german, but you should understand.

mkvmerge v4.6.0 ('Still Crazy After All These Years') gebaut am Mar 10 2011 23:46:43
Fehler: Die Quelldatei '01' konnte nicht geöffnet werden, oder aber die Größenabfrage schlug fehl.

I'm sorry but you are not acutally calling mkvmerge in this for loop.  You must have forgotten to write it...

Now the line "echo mkvmerge -o $mkvfile $filepro $srtfile" outputs a perfectly healthy line

mkvmerge - o 01_-_Title.mkv "01 - Title.ogm" "01 - Title.srt"

I solved the problem, but I still don't understand why it came up after all. I did the following: Instead of embedding the quote signs into the variable, I simply added them to the commandline. It now looks like this:

# !/bin/bash

for ogmfile in *.ogm
    do
        mkvfile=$(echo $ogmfile | sed s/ogm/mkv/)
        srtfile=$(echo $ogmfile | sed s/ogm/srt/)
        echo "Processing $file with $srtfile and output to $mkvfile"
        mkvmerge -o "$mkvfile" "$ogmfile" "$srtfile"
    done

While "echo mkvmerge -o $mkvfile "$filepro" "$srtfile" outputs a line without quote signs at all (like it looked when I completely ignored the blanks) the script runs as it should.

Now: Why can't I store a quote mark inside a variable to create a command line for further re-use like this:

commandline="-o $mkvfile $filepro $srtfile"

If you store the " bash will treat it as a character in that string!!!. You do not need that to work with spaces in file names.

Where do I think the wrong way? Am I supposed to escape something? I experimented with escaping the quote sings on different positions, but it didn't work. Any hints on why it is the way it is?

You think wrong in believing you need to add the " for proper handling of the spaces. Bash is perfectly capable of dealing with blank spaces. You just need to quote properly the variable expansion as you did in this last for loop.

Suggesion:

Don't call sed for such basic string processing. The builtin parameter expansion is more than capable of dealing with such things:

mkvfile=$(echo $ogmfile | sed s/ogm/mkv/)

Will give you same results as

mkvfile="${ogmfile/ogm/mkv}"

With the difference that this does not require neither pipes nor calling an external program.

Last edited by Damnshock (2011-04-16 12:20:51)


My blog: blog.marcdeop.com
Jabber ID: damnshock@jabber.org

Offline

#3 2011-04-16 12:39:51

Awebb
Member
Registered: 2010-05-06
Posts: 6,683

Re: [SOLVED][BASH] strange behavior of quote signs in variables

Thank you. I think I got it now how strings in variables are handled in bash.

About not calling mkvmerge: I accidentally posted the script during debugging. Corrected :-D

Aaaand thank you for the hint on saving pipes. That was actually very helpful. Now I'm busy optimizing my other scripts.

Marked as Solved.

Offline

Board footer

Powered by FluxBB