LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 02-12-2005, 10:25 AM   #1
prx
LQ Newbie
 
Registered: Feb 2005
Posts: 8

Rep: Reputation: 0
odd recursion: calling "by hand" vs calling by cronscript...


I have a cronscript which checks (then fixes) users directories for badly named files - meaning I don't want to allow spaces or ampersands in file or directory names & it will replace spaces with underscores and removes ampersands.

If I have a directory uploaded by a user like this:

/var/pub/ftp/username/top level/second level/third level/
/var/pub/ftp/username/top level/second level/third level/pork & beans


then I call this script by typing it's name (I call it sr) in a bash shell like this:

cd /var/pub/ftp
sr

then I end up with:
/var/pub/ftp/username/top_level/second_level/third_level/ /var/pub/ftp/username/top_level/second_level/third_level/pork_beans

the program works by parsing directories then recursively calling itself to descend further until it has fix all files and directories below the directory it has been called.

I set it to run in a cronscript, and get different results! Here is what I get when it is called by cron:
/var/pub/ftp/username/top_level/second_level/third level/
/var/pub/ftp/username/top_level/second_level/third level/pork & beans

so, it doesn't successfully call itself when started by cron.

Here is the script for sr (called that originally for space replace)
and if you can give me a hint why this might be happening, I would be very grateful.

I just don't get why the recursion works when typed into bash, but not when called by cron

Running FC2, btw.
The cronjob belongs to root.

----

for i in *
do
if [ -d "$i" ] # if * is a directory
then
cd "$i" # descend into the directory
for y in *
do
tempa=$(echo $y | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g')
wait
if [ "$y" != "$tempa" ]
then
mv "$y" "$tempa"
wait
fi
if [ -d "$tempa" ] # if this is also a directory, call this program
then
cd "$tempa"
sr # this is the name of THIS program, must be in your PATH
wait
cd ..
fi
done
cd ..
fi
tempa=$(echo $i | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g')
wait
if [ "$i" != "$tempa" ]
then
mv "$i" "$tempa"
wait
fi
done

Thanks for you help,

Paul in Colorado

Last edited by prx; 02-12-2005 at 10:28 AM.
 
Old 02-12-2005, 11:48 AM   #2
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, SolydK 2013
Posts: 7,434

Rep: Reputation: 134Reputation: 134
"I just don't get why the recursion works when typed into bash, but not when called by cron"

Cron jobs run with a skimpy PATH. You might have the case where sr is on the bash PATH but not on the cron PATH. If that is the problem then you can fix it either by setting the PATH in sr or by invoking sr using the full path name.

Also I do not think that your script includes the logic for the case where sr changes a file name to an already existing file name.

---------------------------
Steve Stites
 
Old 02-12-2005, 02:14 PM   #3
prx
LQ Newbie
 
Registered: Feb 2005
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks so much! That did the trick!

-Paul
 
Old 02-12-2005, 02:19 PM   #4
prx
LQ Newbie
 
Registered: Feb 2005
Posts: 8

Original Poster
Rep: Reputation: 0
also, while we are on the subject....

Is there an easy way to import the bash PATH to the cron PATH at the beginning of a cron script?
 
Old 02-12-2005, 04:59 PM   #5
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, SolydK 2013
Posts: 7,434

Rep: Reputation: 134Reputation: 134
"Is there an easy way to import the bash PATH to the cron PATH at the beginning of a cron script?"

There is a place in the crontab entry where you name the command you wish to run. You can make this entry a series of commands separated by semi colons ;. Set the PATH and then invoke sr.

If you mean can you use the same PATH as set by root bash the answer is "I don't know". Cron runs as its own daemon. It is not the user root although cron runs as ID 0. Finding out what PATH the user root uses and setting that PATH in the cron daemon is probably more trouble than it is worth. Anyway, I don't know how to do it off the top of my head.

---------------------------
Steve Stites

Last edited by jailbait; 02-12-2005 at 05:05 PM.
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
odd "pppd call" behaviour in bash on fc3 kijoma Linux - Networking 1 08-19-2005 05:57 PM
Odd Problem KDE doesn't work with "big desktop" xanas3712 Slackware 0 09-21-2004 11:05 PM
How do you use " ./ or ../ " in calling a directory? Tiyogi Programming 4 06-22-2004 12:57 PM
My computer is calling itself "localhost" in windows david_bridges Linux - Networking 2 12-21-2003 07:15 PM
Networking "by hand" questions . . in Mandrake 9.0 King V Linux - Networking 2 03-12-2003 07:54 AM


All times are GMT -5. The time now is 05:55 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration