Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-25-2007, 06:09 PM   #1
LQ Newbie
Registered: Mar 2007
Posts: 4

Rep: Reputation: 0
Perl error in a Bash script

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?

# 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`

echo "#!/bin/bash" >
echo "" >>

find . -depth -type f -print | while read FILE
		set -x
		while read -r REGEX REPLACE line
				# line 32: perl: command not found
				NEWNAME=$(echo "$OLDNAME" | perl -p -e "s/$REGEX/$REPLACE/g")
		done < "$PRESET"
		printf "mv %-100s \"%s\"\n" "\"$PATH/"$FILE"\"" \""$PATH"/"$NEWNAME"\" >>

find . -depth -type d -print | while read DIR
		echo "DIR -> $DIR"
		while read -r REGEX REPLACE line
				#set -x
				# Line 49 works just fine
				NEWNAME=$(echo "$OLDNAME" | perl -p -e "s/$REGEX/$REPLACE/g")
		done < "$PRESET"
		printf "mv %-90s \"%s\"\n" "\"$DIR\"" "$NEWNAME" >>

Old 03-25-2007, 06:48 PM   #2
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,166

Rep: Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373
It's because you did
PATH is a shell variable that is used to find executable programs. So could either use a different variable name (eg: _PATH, or path), or use the fully qualified path name: /usr/bin/perl instead of perl. I'd recommend choosing the first option, changing PATH like that is asking for trouble.
Old 03-26-2007, 02:40 PM   #3
LQ Newbie
Registered: Mar 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks ntubski.

You were correct. I overlooked the PATH variable name, which conflicted with the environment PATH variable. The script works without error now.



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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
embedded bash in perl script paul_mat Linux - Software 3 11-21-2005 01:46 PM
Help: Perl or Bash Script help embsupafly Programming 1 08-11-2005 06:01 PM
Embedding perl in bash script MikHud Programming 4 02-02-2005 06:26 AM
writing a bash/ perl script on debian phr00ta Programming 4 01-29-2004 01:38 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:56 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