Help with perl and if file statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with perl and if file statement
# 1  
Old 09-01-2009
Help with perl and if file statement

Hello and thank you in advance.

I have a perl script that will basically copy files out of a directory , creat a new current date stamped dir and move them that current time stamped dir and append the file names with current time stamp.

I need to add line(s) that this is only run if FileX is present in directory www/steve . Can anyone help with that. This is will be a cronjob, if that matters.

Code:
#! /usr/local/bin/perl
use strict;
my $origpath = "/home/www/steve/";
my $destpath = "/home/www/steve/backup";
my $date = `date +%Y-%m-%d_%H.%M.%S`;
my $dir = `date +%Y-%m-%d_%H.%M`;
`mkdir $destpath/$dir`;
chomp($date);
chomp($dir);
my @list = `cd $origpath; ls -1 *jpg`;
foreach my $i (@list) {
        chomp($i);
        $i =~ s/.jpg//;
#       print qq {mv "$origpath$i.jpg" "$destpath/$dir/$i-$date.jpg"\n\n};
 `mv "$origpath$i.jpg" "$destpath/$dir/$i-$date.jpg"`;
}

Thank you again!!!
# 2  
Old 09-01-2009
Code:
#! /usr/local/bin/perl
use strict;

my $origpath = "/home/www/steve/";
my $destpath = "/home/www/steve/backup";
my $date = `date +%Y-%m-%d_%H.%M.%S`;
my $dir = `date +%Y-%m-%d_%H.%M`;

`mkdir $destpath/$dir`;
chomp($date);
chomp($dir);

my @list = `cd $origpath; ls -1 *jpg`;

if (-e "/www/steve/FileX"){
   foreach my $i (@list) {
      chomp($i);
      $i =~ s/.jpg//;
#       print qq {mv "$origpath$i.jpg" "$destpath/$dir/$i-$date.jpg"\n\n};
      `mv "$origpath$i.jpg" "$destpath/$dir/$i-$date.jpg"`;
   }
}
else {
   #some other action if the file is not there - or exit;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

Perl - what does this statement mean -Regex

push @MACARRAY, "$+{catalog} $+{machine}\n" if ($info =~ /(?<catalog>catalog).+?(?<machine>\*+)/ms); I am (still) trying to solve problem. Looking around on the server I found this piece of code. Specifically what does "$+{catalog} $+{machine}\n" do ? Thanks in advance (1 Reply)
Discussion started by: popeye
1 Replies

3. Shell Programming and Scripting

Understanding perl statement

can someone help me how to interpret this line? my ($class, $hashref) = @_; my $portfolio = {}; if ($hashref->{portfolio_id}) { ($portfolio) = GEmySQL->get ("select * from portfolio where portfolio.id=$hashref->{portfolio_id}"); } =============== Question: how do... (2 Replies)
Discussion started by: onlinelearner02
2 Replies

4. UNIX for Dummies Questions & Answers

Help with Perl If statement

Hi All, I am writing a perl script where I take 2 variables from the user as STDIN to scan the lines of a file to produce as output. How can I do an IF loop to test this for example in the mock file 12 10 35 20 37 5 45 12if I take user input as 40 and 10, how can I get the output lines in... (3 Replies)
Discussion started by: pawannoel
3 Replies

5. Shell Programming and Scripting

Perl nested if statement

I'm just having a bit of trouble running this code. It tells me that there's a syntax error on line 29. Any help appreciated. #!/usr/bin/perl # # Phone Book Application # %phonebook = ( "Wayne", '34687368', "Home", '378643287', "Work", '017374637', "School",... (2 Replies)
Discussion started by: cabaiste
2 Replies

6. Shell Programming and Scripting

Condition statement in perl

#!/usr/bin/perl $output1 = "/home/log.txt" $output2 = "/home/grep.txt" #Statement1 creates an output file called log.txt. #Statement2 greps a line from log.txt and store the result in grep.txt I want to create a condition where if the file grep.txt is empty repeat process. Thanks. (1 Reply)
Discussion started by: sureshcisco
1 Replies

7. Shell Programming and Scripting

Perl - automating if statement test

Hello all, I'm trying to automate an if statement in my Perl script. The script opens an input file for reading, checks each line in the file for a particular substring, and if it finds the substring, writes it to an output file. There are approximately 200 different input files. Each has... (3 Replies)
Discussion started by: Galt
3 Replies

8. Shell Programming and Scripting

help with if statement perl scripting

Sorry for the newbie question I need to create a perl script to check the modifed time of a file I already have that part created but need help in creating a if statement that basicly will check to see if the modifed time is at least 2hrs older then the current time if the stament is true to... (2 Replies)
Discussion started by: nettech207
2 Replies

9. Shell Programming and Scripting

Problem with if statement in perl

if (($fields eq $hwp) && ($fields eq 'Y')) { $fields = "INTEGRAL"; } elsif ($fields eq $hwp) { $fields = "INTEGRAL"; } elsif ($fields ne $hwp) { $fields = "SEPARATE"; } print "$fields $fields $fields\n"; Output: The problem here is that the first... (2 Replies)
Discussion started by: kamitsin
2 Replies

10. Shell Programming and Scripting

perl if statement

Hello guys, I was wondering why my code doesn't read a variable when using if statement as follows: $userlist='users.txt'; $user='b999'; open (ACTULOG, ">>$userlist"); print ACTULOG "$user\n"; close (ACTULOG) this will work and prints... (3 Replies)
Discussion started by: Bashar
3 Replies
Login or Register to Ask a Question