LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 07-15-2010, 07:36 AM   #1
gohmifune
Member
 
Registered: Nov 2006
Distribution: Ubuntu
Posts: 42

Rep: Reputation: 15
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?
 
Old 07-15-2010, 09:52 AM   #2
sanjayadash
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 1
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.
 
Old 07-16-2010, 12:27 AM   #3
gohmifune
Member
 
Registered: Nov 2006
Distribution: Ubuntu
Posts: 42

Original Poster
Rep: Reputation: 15
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?
 
Old 07-16-2010, 03:06 AM   #4
sanjayadash
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 1
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?
 
1 members found this post helpful.
Old 07-18-2010, 12:07 AM   #5
gohmifune
Member
 
Registered: Nov 2006
Distribution: Ubuntu
Posts: 42

Original Poster
Rep: Reputation: 15
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?
 
Old 07-19-2010, 03:34 AM   #6
sanjayadash
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 1
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.
 
Old 07-20-2010, 01:07 AM   #7
gohmifune
Member
 
Registered: Nov 2006
Distribution: Ubuntu
Posts: 42

Original Poster
Rep: Reputation: 15
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.
 
Old 04-13-2011, 08:12 AM   #8
lurko
Member
 
Registered: Jun 2006
Location: Ontario, Canada
Distribution: Debian
Posts: 448

Rep: Reputation: 35
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.
 
Old 04-13-2011, 08:19 AM   #9
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
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).
 
1 members found this post helpful.
Old 04-13-2011, 08:37 AM   #10
lurko
Member
 
Registered: Jun 2006
Location: Ontario, Canada
Distribution: Debian
Posts: 448

Rep: Reputation: 35
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
bash script, understanding variables antis Programming 5 11-15-2007 04:27 AM
Bash script environment variables mbjunior99 SUSE / openSUSE 4 12-28-2005 12:40 AM
bash script variables twantrd Programming 7 11-17-2004 02:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration