• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 133
  • Last Modified:

Positioning of paramters gets messed up?

Hi, I'm a newbie to perl scripting and i'm trying to do my a script to transfer all files of a given extension to a given directory by calling
"movedir <directory> <extension>"

This is my complete script:

$dir=shift @ARGV;
$ext=shift @ARGV;
chomp($ext);
chomp($dir);
open (FROM,"ls | grep $ext|");
my @results;
$index=0;
foreach(<FROM>)
{
        $results[$index]=$_;
        $index++;
}
foreach(@results)
{
       system("mv","$_ $dir");
}

--------------------------------------------
the result is an error by the mv command.

to see what "mv" command my script was issueing, i substituted system("mv","$_, $dir") with
print "mv $_ $dir" and the output was strange.
it became "<$dir>mv <$_>"

could anyone explain why the position of the paramters to print got messed up?
0
damnedsoul
Asked:
damnedsoul
  • 2
  • 2
1 Solution
 
PC_User321Commented:
$dir=shift @ARGV;
$ext=shift @ARGV;
opendir FROM, "." or die "Could not open $dir: $!";

foreach(readdir FROM)
{
      if (/\.$ext$/) {
            system("mv","$_ $dir");
      }
}
0
 
PC_User321Commented:
>> could anyone explain why the position of the paramters to print got messed up?  
Probably perl had difficulty opening the file called "ls | grep $ext|".

Note the "or die ..." that I used - that is a very useful diagnostic.
Also, run perl with the -w switch to get warnings for things like attempting to read from unopened file handles, as you were attempting to do.
0
 
jmcgOwnerCommented:
The peculiar output is most likely caused by the fact that $_ in your original code still contains a line termination character that it picked up when reading the pipe.

The error message from the mv command is caused by the fact that you are supplying only one argument, not the two that the command is expecting.

I like PC_User321's idea of using readdir instead of the pipeline. If an external command can do something easily that would take a bunch of Perl code to replicate, use the external command. When it's easy to do in Perl, it's faster to skip the external command.

$dir=shift @ARGV or die "Usage: $0 _dir_ _ext_";
$ext=shift @ARGV or die "Usage: $0 _dir_ _ext_";
opendir FROM, "." or die "Could not open $dir: $!";

foreach(readdir FROM)
{
           rename $_, "$dir/$_" if /\.$ext$/;
}
0
 
jmcgOwnerCommented:
OK, OK, now I tested it. What I gave you should work, but I now like this version even better.

#! /usr/bin/env perl

$dir=shift @ARGV or die "Usage: $0 _dir_ _ext_";
$ext=shift @ARGV or die "Usage: $0 _dir_ _ext_";
opendir FROM, "." or die "Could not open $dir: $!";

foreach (glob "*.$ext") {
          rename $_, "$dir/$_";
   }
0
 
damnedsoulAuthor Commented:
Thank you all for your prompt response!
and also the extra info =)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now