Saturday, November 3, 2007

BASH Script to Generate files from existing ones with a partial change in filename

#!/bin/bash
# BASH Script to generate $EXTENSION files like FILE_fr.$EXTENSION,FILE_sp.$EXTENSION from existing FILE_$SEARCHSTRING.$EXTENSION without causing any kind of overwrite.
DIR=/tmp/html
EXTENSION=extension
SEARCHSTRING=en
cd $DIR > /dev/null 2> /dev/null
count=`find *$SEARCHSTRING.$EXTENSION | wc -l`
n=1
echo $count \"*$SEARCHSTRING.$EXTENSION\" files found
while [ $n -le $count ]
do
{
filename=`find *$SEARCHSTRING.$EXTENSION | head -$n | tail -1`
echo $filename
language=(fr sp po)
for (( i = 0 ; i < ${#language[@]} ; i++ ))
do

value=`ls $filename | awk -F "$SEARCHSTRING.$EXTENSION" {'print $1'}`
find "$value""${language[$i]}".$EXTENSION > /dev/null 2> /dev/null
if [ $? != 0 ]
then
{
cp "$value"$SEARCHSTRING.$EXTENSION "$value""${language[$i]}".$EXTENSION
}
fi
done
n=$[$n + 1]
}
done
cd - > /dev/null 2> /dev/null
exit

No comments: