My Bash script utilizes Perl for parsing and replacing parts of filename. I choose Perl for PCRE, yet have not fully committed to a strict Perl solution. I am hoping to learn more about Bash; then I will rewrite this script in Perl and Python.
My Bash script returns a Perl not found error, which is odd since Perl is on the system. The script is a file and folder renamer. It uses a preset file with regular expression patterns. I already created several presets files for different renaming task. These patterns are not as simple as tr ‘A-Z’ ‘a-z’. So SED became cumbersome with its different escaping structure.
The script contains similar code for renaming the files and folders with the Perl line. The folder code processes the directories and does not return an error. But the file part, which has nearly identical code, returns the perl error.
It might have something to do with the find part, but obviously I have no clue. Please have a look at the simplified code below. Let know your thoughts. Does it create issues using perl in a Bash Script? Why does the folder block process without error?
Code:
#!/usr/bin/bash
# Title: File Renamer
# Purpose: This script renames folders and files.
# Its primary feature involves placing all regex
# patterns in another file separated by whitespace.
# This functionality allows one to have presets for
# renaming your folders, mp3, html, php, or css files.
#
#
# sh FileRenamer
#
# Created: 03-07-07
# Modified: 03-08-07
SCRIPTNAME=`basename $0`
REQPARAM=1
PRESET="$HOME/bin/preset.txt"
echo "#!/bin/bash" > doit.sh
echo "" >> doit.sh
find . -depth -type f -print | while read FILE
do
set -x
PATH=${FILE%/*}
FILE=${FILE##*/}
OLDNAME="$FILE"
NEWNAME="EMPTY"
while read -r REGEX REPLACE line
do
# line 32: perl: command not found
NEWNAME=$(echo "$OLDNAME" | perl -p -e "s/$REGEX/$REPLACE/g")
OLDNAME="$NEWNAME"
done < "$PRESET"
printf "mv %-100s \"%s\"\n" "\"$PATH/"$FILE"\"" \""$PATH"/"$NEWNAME"\" >> doit.sh
exit
done
find . -depth -type d -print | while read DIR
do
echo "DIR -> $DIR"
DIR=${DIR##*/}
OLDNAME="$DIR"
NEWNAME="EMPTY"
while read -r REGEX REPLACE line
do
#set -x
# Line 49 works just fine
NEWNAME=$(echo "$OLDNAME" | perl -p -e "s/$REGEX/$REPLACE/g")
OLDNAME="$NEWNAME"
done < "$PRESET"
printf "mv %-90s \"%s\"\n" "\"$DIR\"" "$NEWNAME" >> doit.sh
done
# END