2

I'm just starting out using args in BASH and am trying to script setting the 'Title' tag on a given .MKV to match its filename: less the extension. The command...

mkvpropedit "Good Life.mkv" --edit info --set "title=Good Life"

...works without issue and (thanks to another answered StackOverflow question), I can pull in the full filename - with spaces and extension - and reliably split it into the full filename (i.e. "Good Life.mkv") and the filename w/out extension: i.e. "Good Life". The full script (mkvRetitle.sh) is...

#!/bin/bash
  echo "Change .MKV title to match its filename"
  eval fileWhole=\${1}
  eval fileTitle=$(echo "\${1%.*}")
#  echo $fileWhole
#  echo $fileTitle
  mkvpropedit fileWhole --edit info --set "title=$fileTitle"

The actual calling of 'mkvpropedit' in my script, however, errors out. mkvpropedit returns Error: The file 'fileWhole' is not a Matroska file or it could not be found. I've tried ideas found in Passing a string with spaces as a function argument in bash

but have had no luck and can find nothing else that looks likely: from other sites. I'd really appreciate the help. Thank you very much.

Vikrant More
  • 5,182
  • 23
  • 58
  • 90
malkortechie
  • 21
  • 1
  • 2
  • Didn't fix it, but I found one (unrelated) mistake. Scripted call now reads... mkvpropedit $fileWhole --edit info --set "title=$fileTitle". Error now reads... bash-4.3$ mkvRetitle.sh 'Big Balls.ogg' Change .MKV title to match its filename Error: More than one file name has been given ('Big' and 'Balls.ogg'). – malkortechie Sep 12 '17 at 21:00
  • 2
    Why in the world do you have all this `eval` nonsense? (If another SO question's answer advised you to use `eval`, the quality of that answer is... [suspect](http://mywiki.wooledge.org/BashFAQ/048)). – Charles Duffy Sep 12 '17 at 21:11
  • 2
    I agree with @CharlesDuffy about the `eval`s (they're useless at best, dangerous at worst). Also, `$(echo ...)` is useless, since the `$( )` and `echo` pretty much cancel each other out. `echo` takes a string and prints it as output, and `$( )` takes that output and turns it back into a string. Just use the string directly! – Gordon Davisson Sep 13 '17 at 02:44

1 Answers1

4

Whenever you have to deal with variables/arguments containing white space, just put quotes around it, that's it.

#!/bin/bash
echo "Change .MKV title to match its filename"
fileWhole="$1"
fileTitle="${1%.*}"
echo "$fileWhole"
echo "$fileTitle"
mkvpropedit "$fileWhole" --edit info --set "title=$fileTitle"
yacc
  • 2,915
  • 4
  • 19
  • 33
  • 1
    Should quote the arguments to `echo` too. `echo "$fileWhole"` can be different from `echo $wholeFile` if the file's name contains glob expressions, runs of multiple spaces, alternate whitespace such as tabs, etc. – Charles Duffy Sep 12 '17 at 21:49