LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-25-2007, 06:09 PM   #1
rujin69
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?

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
 
Old 03-25-2007, 06:48 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
It's because you did
Code:
PATH=${FILE%/*}
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
rujin69
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.

Cheers!
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 PM.

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