[英]How do I execute a unix command containing a perl variable in perl
In the following perl code, I am tring to copy a perl variable $file from one directory to another directory with: 在下面的perl代码中,我打算使用以下命令将一个perl变量$ file从一个目录复制到另一个目录:
"system("cp $file $Output_Dir);
This command writes down the file name alright but then says: 此命令会记下文件名,但会显示:
cp: cannot stat 'tasmax_AFR-44_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_CLMcom-CCLM4-8-17_v1_day_19910101-19951231.nc': No such file or directory
The command 命令
system("@sixfiles = ls $Vars[$kk]}*");
gives me the error: sh: 1: =: not found I wonder what is wrong with this code. 给我错误:sh:1:=:找不到我想知道这段代码出了什么问题。 Assistance will be appreciated. 协助将不胜感激。
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Path;
use File::Copy;
my $debug = 1;
my @Vars = ("pr","tasmin","tasmax");
my $Vars;
my @sixfiles;
my $sixfiles;
my $Input_Dir = "/home/zmumba/DATA/Input_Dir";
my $Output_Dir = "/home/zmumba/DATA/Output_Dir";
for (my $kk=0; $kk < @Vars; ++$kk) {
opendir my $in_dir, $Input_Dir or die "opendir failed on $Input_Dir: $! ($^E)";
while (my $file=readdir $in_dir) {
next unless $file =~ /^$Vars[$kk]/;
next if -d $file;
print "$file\n";
print "Copying $file\n" if $debug;
my $cmd01 = "cp $file $Output_Dir";
print "Doing system ($cmd01)\n" if $debug;
system ($cmd01);
system("@sixfiles = ls $Vars[$kk]}*");
}
}
Try this: 尝试这个:
use feature qw(say);
use strict;
use warnings;
use File::Spec;
my @Vars = ("pr","tasmin","tasmax");
my $Input_Dir = "/home/zmumba/DATA/Input_Dir";
my $Output_Dir = "/home/zmumba/DATA/Output_Dir";
opendir my $in_dir, $Input_Dir or die "opendir failed on $Input_Dir: $! ($^E)";
while (my $file=readdir $in_dir) {
next if ($file eq '.') || ($file eq '..');
next if -d $file;
next if !grep { $file =~ /^$_/ } @Vars;
say "Copying $file";
$file = File::Spec->catfile( $Input_Dir, $file );
system "cp", $file, $Output_Dir;
}
system ($cmd01);
Gives: 给出:
cp: cannot stat '<long-but-correct-file-name>': No such file or directory
This is almost certainly because you are not running the code from $Input_Dir
, so that file doesn't exist in your current directory. 几乎可以肯定这是因为您没有运行$Input_Dir
的代码,因此该文件在当前目录中不存在。 You need to either chdir
to the correct directory or add the directory path to the front of the file name variable. 您需要chdir
到正确的目录,或将目录路径添加到文件名变量的前面。
system("@sixfiles = ls $Vars[$kk]}*");
This code makes no sense. 此代码没有任何意义。 The code passed to system()
needs to be Unix shell code. 传递给system()
的代码必须是Unix shell代码。 That's the ls $Vars[$kk]}*
bit (but I'm not sure where that }
comes from). 那是ls $Vars[$kk]}*
位(但是我不确定}
来源)。 You can't populate a Perl array inside a shell command. 您不能在shell命令中填充Perl数组。 You would need to capture the value returned from the ls
command and then parse it somehow to separate it into a list. 您将需要捕获ls
命令返回的值,然后以某种方式解析它以将其分成一个列表。
You can give a try with the following code: 您可以尝试使用以下代码:
#!/usr/bin/env perl
use strict;
use warnings;
my $debug = 1;
my @Vars = ("pr", "tasmin", "tasmax");
my $Vars;
my $Input_Dir = "/home/zmumba/DATA/Input_Dir";
my $Output_Dir = "/home/zmumba/DATA/Output_Dir";
my $cpsrc, $cpdest = '';
print "No Write Permission: $!" unless(-w $Output_Dir);
for my $findex (0 .. $#Vars) {
$cpsrc = qq($Input_Dir/$Vars[$findex]);
print "$Vars[$findex]\n";
print "Copying $Vars[$findex]\n" if $debug;
my $cmd01 = "cp $cpsrc $Output_Dir";
print "Doing system ($cmd01)\n" if $debug;
system($cmd01);
}
You don't have to go through each file in source dir. 您不必遍历源目录中的每个文件。 You already know the files to copy from source. 您已经知道要从源复制的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.