ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Objective: Create a perl daemon which will check a folder for the presence of a file, then move it to another folder based on extension. Extensions in use are .mp3, .wav, .txt.
I kludged together this script:
#!/usr/bin/perl
# Scripts to monitor Enco xfer folder and move the files to
# the proper folders on the Enco side.
#
# I hope!
#
# by Michael Barnes
#
#
use File::Copy;
use POSIX qw(setsid);
chdir '/' or die "Can't chdir to /: $!";
umask 0;
# next 4 lines commented for debugging
# open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
# open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
# open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!";
# defined(my $pid = fork) or die "Can't fork: $!";
unless(my $pid = fork()) {
exit if $pid;
setsid or die "Can't start a new session: $!";
while(1) {
sleep(5);
print "Hello...\n"; # for debugging
undef (@filelist); # attempt to clear array
@filelist = glob "/mnt/xfer/Enco/xfer/*";
@logs = grep (/txt|TXT/, @filelist); # cull out text files
@mp3 = grep (/mp3|MP3/, @filelist); # cull out mp3 files
@wav = grep (/wav|WAV/, @filelist); # cull out wav files
foreach $_ (@logs) {
push @logfile,$_;
}
foreach $logfile (@logfile) {
$newlogfile = $logfile;
$newlogfile =~ s/\/mnt\/xfer\/Enco\/xfer/\/tmp\/Data\/logs/;
print "'$logfile' and '$newlogfile'\n"; #for debugging
move ($logfile, $newlogfile) or die "Holy moly we got us a log error here hoss!";
}
$logfile = (); # attempt to clear array
undef ($newlogfile); # attempt to clear array
# Following segments for other file types are identical. Deleted for brevity in debugging.
}
}
Problem: Once started, the script runs fine. When some files are placed in the xfer folder they are properly moved to the correct folder and gone from the xfer folder. However, when the script loops through again, it tries to move the files again and crashes on the move since the files are gone.
Thoughts: It seems to me that the arrays are not clearing and are being re-read.
foreach $logfile (@logfile) {
$newlogfile = $logfile;
$newlogfile =~ s/\/mnt\/xfer\/Enco\/xfer/\/tmp\/Data\/logs/;
print "'$logfile' and '$newlogfile'\n"; #for debugging
move ($logfile, $newlogfile) or die "Holy moly we got us a log error here hoss!";
}
$logfile = (); # attempt to clear array
undef ($newlogfile); # attempt to clear array
In the above piece of code, you are setting the scalar $logfile to () and not the array @logfile
i guess that's the problem, just change the variable name or set @logfile to () and see.
ps: I don't have access to a linux system so could not check it myself.
In the above piece of code, you are setting the scalar $logfile to () and not the array @logfile
i guess that's the problem, just change the variable name or set @logfile to () and see.
ps: I don't have access to a linux system so could not check it myself.
Give that man a Kewpie doll!! That was it. A simple change from $ to @ and all works well.
That's why i never use the same (or too similar) names for scalars and arrays.
Also, the way to clear an array is
@arr = ();
you should do this for all the arrays at the top of the loop, just before (or just after) the sleep().
This block is redundant, you've already got an array of logfiles:
foreach $_ (@logs) {
push @logfile,$_;
}
Personally I always use proper names for loop vars, even if $_ is avail, it's just easier to read/debug, especially as progs get longer/more complex.
Always use
#!/usr/bin/perl -w
use strict;
at the top of your progs.
you can do a test compile/syntax check with
perl -wc yourprog.pl
which will check but not run it.
Well, he's got a generic list of all files: @filelist, then he greps out a sub-list for each type of file; @logs, @wav, @mp3 for processing.
Why copy each sub-list again?
BTW, a simpler way is
@arr1 = @arr2;
it's better to get the habit of using a block for map and grep too, as it keeps
your $_ local when you use it. Otherwise if you do a map with a s/// you might clobber
what you don't mean to.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.