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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
07-15-2010, 07:36 AM
|
#1
|
Member
Registered: Nov 2006
Distribution: Ubuntu
Posts: 42
Rep:
|
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: or just 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?
|
|
|
07-15-2010, 09:52 AM
|
#2
|
LQ Newbie
Registered: Dec 2009
Posts: 6
Rep:
|
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.
|
|
|
07-16-2010, 12:27 AM
|
#3
|
Member
Registered: Nov 2006
Distribution: Ubuntu
Posts: 42
Original Poster
Rep:
|
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:
where the script contents are
Code:
program switches filename
What variable would I use to replace filename?
|
|
|
07-16-2010, 03:06 AM
|
#4
|
LQ Newbie
Registered: Dec 2009
Posts: 6
Rep:
|
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.
|
07-18-2010, 12:07 AM
|
#5
|
Member
Registered: Nov 2006
Distribution: Ubuntu
Posts: 42
Original Poster
Rep:
|
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?
|
|
|
07-19-2010, 03:34 AM
|
#6
|
LQ Newbie
Registered: Dec 2009
Posts: 6
Rep:
|
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.
|
|
|
07-20-2010, 01:07 AM
|
#7
|
Member
Registered: Nov 2006
Distribution: Ubuntu
Posts: 42
Original Poster
Rep:
|
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.
|
|
|
04-13-2011, 08:12 AM
|
#8
|
Member
Registered: Jun 2006
Location: Ontario, Canada
Distribution: Debian
Posts: 448
Rep:
|
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.
|
|
|
04-13-2011, 08:19 AM
|
#9
|
Member
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798
Rep: 
|
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.
|
04-13-2011, 08:37 AM
|
#10
|
Member
Registered: Jun 2006
Location: Ontario, Canada
Distribution: Debian
Posts: 448
Rep:
|
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 12:07 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|