Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 12-06-2006, 08:29 PM   #1
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
#! /bin/bash
FILE="basename $INPUT .avi"
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
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,891

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
Maybe something like this:

for i in `ls *.avi`
FILE="basename $i"
ffmpeg -i $FILE -sameq -target ntsc -vcd $FILE.mpg

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
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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

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
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.
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
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.
for i in `ls *.avi`
gives me an output of
when what i want is:
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
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.
You can check "man bash" for details on the IFS environment variable.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Linux Forums > Linux - General

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

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