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.

3
  • 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'). Commented Sep 12, 2017 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). Commented Sep 12, 2017 at 21:11
  • 2
    I agree with @CharlesDuffy about the evals (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! Commented Sep 13, 2017 at 2:44

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.