LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-06-2006, 08:29 PM   #1
mma8x
Member
 
Registered: Jul 2004
Posts: 164

Rep: Reputation: 30
script help for batch processing


i started trying to write a script to batch encode files into ffmpeg. failed miserably, then found tovid and don't really need it anymore. still, i was curious. my problem was that i couldn't find a way to select all files in a directory, perform a command on each, and output each file discreetly. i think the latter problem can be solved with "basename". ideas that failed:
1. since the files were named "1x0[1-23] something.avi", i tried to figure a way to set an input variable as the fourth character, then increment the variable up on the next pass. eg, INPUT=1, then first file name would be "1x0[$INPUT]*.avi". i couldn't figure out how to expand the wildcard though.
2. try to print the filenames directly to the variable. i tried various ways of doing this, but couldn't figure out how to pipe the output of something into a variable.
3. a variation on #1: i was able to get to
Code:
#! /bin/bash
Y=$1
INPUT=1x0$Y*avi
FILE="basename $INPUT .avi"
OUTPUT=$FILE.mpg
ffmpeg -i $INPUT -sameq -target ntsc-vcd $OUTPUT
Y=`expr $Y + 1`
but couldn't figure out how to loop it (i was using "while"). any knowledge would be great.
 
Old 12-06-2006, 08:57 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Maybe something like this:

#!/bin/bash
for i in `ls *.avi`
do
FILE="basename $i"
ffmpeg -i $FILE -sameq -target ntsc -vcd $FILE.mpg
done

cd to the directory containing the .avi files to run the script. `ls *.avi` runs ls and lists the .avi files. Each iteration of the loop sets File equal to the basename of the file listed in that iteration.
 
Old 12-06-2006, 09:26 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Using variable substitution, you can extract certain characters from the variable containing the filename, and use that in your script.

For example, if $input is assigned in a for loop like, "for input in 1x[[:digit:]][[:digit:]]*.avi; do"
and lets say a filename "1x01elephant.avi" is assigned to $input. Then ${input:2:2} will select the 3rd and 4th characters in the variable. Another one that might be useful is ${input#1x[[:digit:]][[:digit:]]} which will strip off the 1x[[:digit:]][[:digit:]] part, leaving only "elephant.avi".

You can enter the output of a command used in a command using either backticks or $(command ... ).
The latter is recommended. It has the advantage where one could be embedded inside another.

for basevid in $( 1x[[:digit:]][[:digit:]]*.avi | sed 's/1x..\(.*\).avi/\1/' | sort | uniq ); do
for vid in 1x[[:digit:]][[:digit:]]$basevid; do
...
done
done

This would allow you to process all files 1x??elephant.avi separately from 1x??zebra.avi.

Sometimes, you might want to build up an array:
zoo=( $(ls 1x[[:digit:]][[:digit:]]*.avi | sed 's/1x..\(.*\)/\1 | sort | uniq) )

Last edited by jschiwal; 12-06-2006 at 09:28 PM.
 
Old 12-07-2006, 05:41 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
For 1 and 3, look at above posts. Variables & wildcards are expanded automatically by the shell, unless you put them inside single quotes. Be careful when using a variable as part of a string, like $Y in your example. To make it clear the "Y" is the name of your variable (not $Y*, $Y*avi or something), you should enclose the Y
in curly braces, ie ...${Y}*.avi...

For 2, and this is done implicitly in the for-loop of bigrigdriver's post:
use backticks (``, which are not the same as single quotes '').
or the $(...) operator.
Example:
Code:
myVar=`echo something`;
myVar2=`echo 1x0..${myVar}.avi`;
"echo $myVar" will then show that the variable contains the string "something".
"echo $myVar2" will show that myVar2 holds the string "1x0..something.avi" (so yes, $myVar is expanded by the backticks).

As a general remark, I suggest you read pieces of "man bash". Especially the parts on quoting and variable syntax for starters.
 
Old 12-07-2006, 05:52 PM   #5
mma8x
Member
 
Registered: Jul 2004
Posts: 164

Original Poster
Rep: Reputation: 30
thanks for the help.
timmeke, yes, i had tried all variety of quotes trying to make it work without success.
jschiwal. that all seems a bit above me at this point. not sure what exactly [[:digit:]] does. took a look at sed, which seems pretty complicated.
bigrigdriver, that seems about my speed, but i'm still having troubles. it seems that spaces (and probably other non-standard characters too) aren't being interpreted correctly.
Code:
for i in `ls *.avi`
gives me an output of
Code:
1x01
,
when what i want is:
Code:
1x01 Juliet Prowse.avi
using ls -b, -Q didn't help. each word is interpreted as a separate variable.
and, tovid isn't giving me the results i want, so that's giving me extra incentive to get it done...
 
Old 12-08-2006, 01:31 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
By default, the shell (ie in your for-loop) splits the 'ls' output on word boundaries like spaces and tabs.
If you only want to split on newlines, like in your case, just put the following command before the for-loop.
Code:
IFS="\n";
You can check "man bash" for details on the IFS environment variable.
 
  


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
Batch Processing ankan Linux - Software 3 10-11-2013 10:52 AM
Image batch processing cvzyl Linux - Software 3 03-20-2006 02:45 AM
how to do batch processing with time out? Franziss Programming 4 11-30-2005 10:17 PM
Change batch script to shell script alan.belizario Programming 5 03-31-2005 12:41 AM
Tune Linux pipe size for batch processing tana Programming 5 12-27-2002 01:09 AM

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

All times are GMT -5. The time now is 03:04 AM.

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