LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Variables and Mkvextract in a bash script and a good resource for bash help? (http://www.linuxquestions.org/questions/linux-general-1/variables-and-mkvextract-in-a-bash-script-and-a-good-resource-for-bash-help-819956/)

gohmifune 07-15-2010 07:36 AM

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?

My thinking is you run either:
Code:

script.sh file.mkv
or just
Code:

script.sh
in a terminal to do all files in a directory.

I want to say the contents would look like
Code:

mkvextract tracks (some file variabe) 3:(same file variable again)
But I don't think that works. Also, any scripting exercise websites?

sanjayadash 07-15-2010 09:52 AM

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.

gohmifune 07-16-2010 12:27 AM

for, do, and done, are a step in the right direction, but I'm getting nothing but parsing errors. I think I should take a step back.

What do I do if I want to run a script on a file? Say:
Code:

script.sh filename
where the script contents are
Code:

program switches filename
What variable would I use to replace filename?

sanjayadash 07-16-2010 03:06 AM

You have to use $1 in place of filename in your approach.

But I would like to know what kind of parsing error you are getting? Is it due to for...do..done or is it coming from mkvextract?

gohmifune 07-18-2010 12:07 AM

The error is coming from mkvextract, it is misinterpreting filenames with spaces as multiple files. file1.mkv works, but not file 1.mkv.

[CODE]#! /usr/bin/ksh

for mkv_files in `ls *.mkv`
do
mkvextract tracks ${mkv_files} 3:${mkv_files}
done[CODE]

Any idea on getting whatever ls brings up as "file 1.mkv" instead of just file 1.mkv?

sanjayadash 07-19-2010 03:34 AM

Hi
 
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.

Hope this helps.

gohmifune 07-20-2010 01:07 AM

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.

lurko 04-13-2011 08:12 AM

not timely...
 
This works with filename-spaces:

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.

j1alu 04-13-2011 08:19 AM

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).

lurko 04-13-2011 08:37 AM

This works great too, thanks for the tip j1alu (and sanjayadash for that matter!):

for mkv_files in *.mkv; do mkvextract tracks "${mkv_files}" 2:"${mkv_files}.ac3"; done


All times are GMT -5. The time now is 09:39 PM.