?
Solved

Positioning of paramters gets messed up?

Posted on 2003-02-19
5
Medium Priority
?
132 Views
Last Modified: 2010-03-05
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
Comment
Question by:damnedsoul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 7980766
$dir=shift @ARGV;
$ext=shift @ARGV;
opendir FROM, "." or die "Could not open $dir: $!";

foreach(readdir FROM)
{
      if (/\.$ext$/) {
            system("mv","$_ $dir");
      }
}
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 7980801
>> 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
 
LVL 20

Accepted Solution

by:
jmcg earned 200 total points
ID: 7982749
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
 
LVL 20

Expert Comment

by:jmcg
ID: 7982825
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
 

Author Comment

by:damnedsoul
ID: 7982987
Thank you all for your prompt response!
and also the extra info =)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question