Variables and Mkvextract in a bash script and a good resource for bash help?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Variables and Mkvextract in a bash script and a good resource for bash help?
So I'm new bash scripting, and what I want to do is write a script that allows me to rip subtitles from various .mkv files using mkvextract(if there is a better tool let me know).
I don't have a good idea about how to go about this, so I'm hoping to be nudged in the right direction, I want to learn this after all. Would I be better off doing this in another language?
Just try this and let us know if it works for you
#! /usr/bin/ksh
for mkv_files in `ls *.mkv`
do
mkvextract tracks ${mkv_files} 3:${mkv_files}
done
Note: I am not very much clear about your requirement. I am also not familiar with mkvextract tool. Execute this script in the directory where all your .mkv files are present.
If your file names are having spaces in between, then you can try while loop instead of for loop.
Here is the code:
ls *.mkv | while read line ; do mkvextract tracks ${line} 3:${line}; done
Note: Output of "ls *.mkv" is given as input to while loop and the read command will read one line at a time and store the value in "line" variable which is you file name with/without spaces.
the previous script doesn't work for me. From what I can tell, it should, but I think it has to do with how linux does filenames. Files without spaces work, but those that do have them don't.
I am guessing that 'while read line' reports filenames as "foo bar 10.mkv" instead of "foo\ bar\ 10.mkv"
Is there a way to append quotation marks to each line, or retain name formatting that uses \. Quotation marks seem like it would be easier.
ls *.mkv | while read line ; do mkvextract tracks "${line}" 3:"${line}"; done
But there's one obvious problem, and it's telling mkvextract to give the extracted track the same filename as the source file. This worked great for me instead:
ls *.mkv | while read line ; do mkvextract tracks "${line}" 3:"${line}.ac3"; done
I was extracting all the ac3 audio tracks, use whatever extension is appropriate for your situation.
I don't think using ls with the wildcard * is a good idea: http://mywiki.wooledge.org/BashPitfalls
pitfall no1. "for i in *" does just the same.
You will also find some good info in general (in the FAQ of the same site).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.