bashmkv

bash script with with 2 user input variables not working


I am using a bash script to remove audio and subtitle tracks from .mkv files with mkvmerge.

Until now, I manually changed the bash script depending on the tracks I wanted to keep.

As this is kinda stupid, I wanted to include user input into the script, which specifies these tracks as well as the file name suffix.

If I only use 1 user input variable for choosing the subtitle track, it works:

#!/bin/bash
echo Which subtitle track would you like to keep?
read subsvar
for file in *mkv; do
    mkvmerge -o "${file%.mkv}".lesssubs.mkv -s $subsvar "$file"
done 

But if I try the same with 3 variables, none of them are used and all audio and subtitle tracks are excluded:

#!/bin/bash

read -p 'Audio track to keep: ' subvar
read -p 'Subtitle track to keep: ' audiovar
read -p 'Filename suffix: ' suffixvar

for file in *mkv; do
    mkvmerge -o "${file%.mkv}".$suffixvar.mkv -a $audiovar -s $subvar "$file"
done 

Solution

  • @Gowiser Thank you very much, the "echo mkvmerge" helped a lot, as I didn't see the very obvious problem without it. I screwed up the variable order (subvar <-> audiovar), which resulted in mkvmerge failing. The correct script should be:

    #!/bin/bash
    
    read -p 'Audio track to keep: ' audiovar
    read -p 'Subtitle track to keep: ' subvar
    read -p 'Filename suffix: ' suffixvar
    
    for file in *mkv; do
        mkvmerge -o "${file%.mkv}"."$suffixvar".mkv -a "$audiovar" -s "$subvar" "$file"
    done 
    

    Thanks again for the quick replies!