PDA

View Full Version : Need help converting PHP/BASH script to pure BASH


altonbr
April 4th, 2008, 12:01 PM
I thought it would be best to split this off from a previous topic I started as it was getting off track from my original question, so I apologize for the double-post.

I wrote a PHP script that was heavily dependant on BASH because I couldn't quite get BASH to do what I wanted. This script will scan a specified folder for all files that end in .wmv or .asf and convert them to .avi using mencoder. I'm now, however, trying to make it purely a BASH script to remove the dependency on php-cli.

My PHP script:
<?php

$extensions = array('wmv', 'asf');
$folder = '/home/brett/torrents/__completed__';

print(' -- Looking in '.$folder.' for all specified files...'."\n");

foreach ($extensions as $ext)
{
$data .= shell_exec('find '.$folder.' -iname *.'.$ext); #load data found by find into $data, case-insensitive
}

if (empty($data))
{
die(' -- No files found. Exiting...'."\n");
}

$files_found = explode("\n",$data); # turn each new line into a piece of an array

foreach ($files_found as $key => $value)
{
if (is_null($value) || $value == '') {
unset($files_found[$key]); # get rid of all empty values
}
}

$file_count = count($files_found);
print(' -- '.$file_count.' file(s) found. Continuing...'."\n");

foreach ($files_found as $file_name)
{
$file_name_escaped = addcslashes($file_name,' &\'"()'); # escape (space),&, \, ", (, ) for Linux CLI
$file_type = shell_exec('file --brief '.$file_name_escaped); # only display file_type output
//print('file --brief '.$file_name."\n"); # for debugging what will be read by file
if(preg_match('/Microsoft/',$file_type)) # make sure the file extension is what is says it is... .wmv and .asf usually returns as 'Microsoft ASF'
{
print(' -- Converting '.$file_name.' ...'."\n"); # show user what we're converting
shell_exec('mencoder '.$file_name_escaped.' -ovc copy -oac copy -o '.$file_name_escaped.'.avi 2>&1 > /dev/null'); # hide output, for aesthetics only
print(' -- Removing '.$file_name.' ...'."\n"); # show user what we're removing
shell_exec('rm -f '.$file_name_escaped); # remove old file
}
}

?>

But I'm already running into problems converting the 'foreach' loop to BASH.

Specifically here:
foreach ($extensions as $ext)
{
$data .= shell_exec('find '.$folder.' -iname *.'.$ext); #load data found by find into $data, case-insensitive
}

if (empty($data))
{
die(' -- No files found. Exiting...'."\n");
}

$files_found = explode("\n",$data); # turn each new line into a piece of an array

foreach ($files_found as $key => $value)
{
if (is_null($value) || $value == '') {
unset($files_found[$key]); # get rid of all empty values
}
}

$file_count = count($files_found);
print(' -- '.$file_count.' file(s) found. Continuing...'."\n");

I want to be able to find all files that end in .wmv and .asf (case-insensitive), which I've done, but then make sure it found at least one file. I would like it to count how many files it found, but it isn't too important. The point of storing the output of 'find' in $data was so I could act on each file individually using mencoder...

This is what I have so far:
$FILENAME=$0
$FOLDER=$1;
$EXTENSIONS=array('wmv', 'asf');

function check_parameters(){
print_func "check_parameters"

if [ $# -ne 1 ]; then
force_exit 1 "Wrong number of parameters; USAGE: ./$FILENAME <folder_to_search>"
fi
}

function search_for_files(){
for EXT in $EXTENSIONS; do
find $FOLDER -iname *.$EXT # how can I get mencoder to act on each file it finds individually?
done
}

function print_func(){
echo " ## $1()"
}

function print_info(){
echo " -- $1, continuing..."
}

function print_warn(){
echo " ** $1, continuing..."
}

function force_exit() {
echo " !! $2, exiting..."
exit $1
}

function safe_exit() {
echo " -- Safely exiting..."
exit 0
}

check_perameters
search_for_files
safe_exit

I appreciate any help you can give me =)

ghostdog74
April 4th, 2008, 12:18 PM
# find /path -maxdepth 1 -type f \( -name "*asf" -o -name "*.wmf" \) -print | while read -r FILE
do
mencoder <options> $FILE
done

or using -exec

# find /path -maxdepth 1 -type f \( -name "*asf" -o -name "*.wmf" \) -exec mencoder <options> "{}" \; #not tested