malkortechie
malkortechie

Reputation: 21

BASH Scripting: take a filename (with spaces) as argument and use name (no ext) and also full filename as args for scripted command

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.

Upvotes: 2

Views: 6773

Answers (1)

yacc
yacc

Reputation: 3376

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"

Upvotes: 4

Related Questions