Help gettgin this script to work on linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help gettgin this script to work on linux
# 1  
Old 08-23-2011
Help gettgin this script to work on linux

This script used to work on windows but is erroring our at line 25 when run under linux. The specific errir that i get is "Can't call method "mtime" on an undefined value at ./make_event_log_index.pl line 25."

Any help would be appreciated

....................................................................................................
Code:
#!/usr/bin/perl -w

use File::Find;
use File::stat;

for ($count = 1; $count <= 23; $count++){
  $#file = -1; # Zero-out array
  $dir = "/home/bed_mfg1/event_logs";
  find(\&Wanted, $dir);
  @file=reverse(sort(@file));
  open (FILE, "> /tmp/event_logs_listing.tmp") or die "Could not open file: $!";
  print FILE "@file";
  close FILE;
  rename "/batch/event_logs_listing.tmp", "/batch/event_logs_listing.txt";
  $#file = -1; # Zero-out array after run (so memory's freed for sleep cycle)
  sleep 3300;  # Roughly the amount of time, when added to execution time, to equal an hr
}


sub Wanted
  {
    my $name = $File::Find::name;
    $name =~ s/\//\\/g;
    $size = -s "$name";
    $date = stat($name)-> mtime;
    my ($sec, $min, $hour, $day, $month, $year) = (localtime($date))[0,1,2,3,4,5,6];
    $year+=1900;
    $month++;  # I have _NO_ idea why $month is off by one.  None.
    if ($min < 10){$min = "0".$min;}
    if ($hour < 10){$hour = "0".$hour;}
    if ($month < 10){$month = "0".$month;}
    if ($day < 10){$day = "0".$day;}

    $name =~ s/\\/\\\\/;
    $file[$#file+1]  = "$year-$month-$day".'_'."$hour:$min".",".$name.",".$size."\n";
  }


Last edited by pludi; 08-23-2011 at 12:03 PM..
# 2  
Old 08-23-2011
Because 2 lines above that you're replacing all forward slashes (/) with backslashes (\), which are an escape character on all Unix platforms, not path separators.

And about your comment on why $month is "off by one": by convenience since I don't know when, as you can use the value directly as an array index for month names (as outlined in the documentation for localtime), and you can use it in mod 12 calculations without having to increment/decrement all the time.

The last line, by the way, could be written much more elegantly by using push and sprintf Smilie
# 3  
Old 08-23-2011
Remove that $name =~ s/\//\\/g; regex, UNIX doesn't need it. You're replacing all the forward slashes with back slashes, causing invalid\paths\like\this to be fed into stat().

[edit] Curses, foiled again.
# 4  
Old 08-23-2011
BTW, if you're developing for multiple platforms, you might want to take a look at File::Util (sadly, not part of the core modules), which will do all this mapping for you automagically.

Also: There can be only one! [cue Thunder sound]
# 5  
Old 08-23-2011
Your code is unsecure against symbolic links that point to non-existing files.
Try this modified subroutine:
Code:
sub Wanted
{
my $name = $File::Find::name;

unless($st = stat($name)) {
  print STDERR "could not stat file: $name\n";
  return;
}
$size = $st->size;
$date = $st->mtime;
my ($sec, $min, $hour, $day, $month, $year) = (localtime($date))[0,1,2,3,4,5,6];
$year+=1900;
$month++; # I have _NO_ idea why $month is off by one. None.
if ($min < 10){$min = "0".$min;}
if ($hour < 10){$hour = "0".$hour;}
if ($month < 10){$month = "0".$month;}
if ($day < 10){$day = "0".$day;}

$file[$#file+1] = "$year-$month-$day".'_'."$hour:$min".",".$name.",".$size."\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

My script work on Linux but not work in sunos.

My script work on Linux but not work in sun os. my script. logFiles="sentLog1.log sentLog2.log" intial_time="0 0" logLocation="/usr/local/tomcat/logs/" sleepTime=600 failMessage=":: $(tput bold)Log not update$(tput rmso) = " successMessage="OK" arr=($logFiles)... (7 Replies)
Discussion started by: ooilinlove
7 Replies

2. What is on Your Mind?

[?] Does Netflix work on Linux in Firefox?

I've tried to totally ignore Netfix to date, though I might change my mind. But I refuse to keep a Windows box running for any reason. Is anyone streaming movies from Netflix and watching them on a Linux box? I went to netflix.com to look for the requirements page. None. OK, how about a... (4 Replies)
Discussion started by: KenJackson
4 Replies

3. Shell Programming and Scripting

Modify awk command to work in linux

Hi All I have the original file like '1','3','COMMON','N','djpCM3PopDimDateGen','JobDescription','1','ds','O','ONE' '1','3','COMMON','N','djpCM3PopDim1','JobDescription','2','ds','O','ONE' '1','3','COMMON','N','djpCM3PopDim2','JobDescription','3','ds','O','ONE'... (3 Replies)
Discussion started by: vee_789
3 Replies

4. Shell Programming and Scripting

sed works on Linux and Unix does not work

Hi, I use this command in Linux but if I run the same command does not work in freebsd. Follow the below command: Linux works: sed -e '1731a\' -e '####' squid.conf > squid2.conf ; sed -e '1731a\' -e 'acl TESTE_ip src 192.168.1.1/255.255.255.255' squid2.conf > squid.conf ; sed -e... (7 Replies)
Discussion started by: andreirp
7 Replies

5. UNIX for Dummies Questions & Answers

Any Linux/Unix work with SATA yet?

i got my computer in 2k, built it myself. top of the line then and better than most still now. one problem however is i was never able to install unix because the old kernels were not compatible with SATA hard drives i dont have any IDE drives nor do i want any I want mine on SATA, but every... (5 Replies)
Discussion started by: GXDeMoNN
5 Replies

6. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

7. UNIX for Dummies Questions & Answers

Work on Linux Using Bootable CD ??

Hi , I am using a laptop with windows XP as the operating system. i want to use linux/unix without installing it on my machine. I heard that i can do so using some Linux bootable CD .... which can be used to work on linux environment with out physically installing it on your system. I... (2 Replies)
Discussion started by: newbie07
2 Replies

8. UNIX for Dummies Questions & Answers

Is it possible to put the whole Linux OS on DVD and work with it?

Is it possible to put the whole Linux OS on DVD and work with it? If yes, how to do it? (1 Reply)
Discussion started by: alexhon
1 Replies

9. UNIX for Advanced & Expert Users

Trying to get my USB network card to work with Linux

Ok, heres the deal... Yesterday i installed Unix on my laptop and surprisingly everything works perfect.....exept my network card i have a linksys USB network card (basically a thing you plug into a USB port that is now your NIC card and i connect it to my router) But i have no idea how i... (1 Reply)
Discussion started by: Freakytah
1 Replies

10. UNIX for Dummies Questions & Answers

ISDN on Linux..will it work?

Hi everyone! I going to change from a 56k modem to a ISDN connection and was wondering if this will work under Linux and what i would need to get it up and running ? (on Mandrake 8.0).. thanx in advance.. grtz phaelanx :) (4 Replies)
Discussion started by: Phaelanx
4 Replies
Login or Register to Ask a Question