Perl system()调用转义元字符

时间:2018-03-15 03:02:15

标签: bash shell perl

我的学生编写了这个简单的程序,它调用系统来捕获一些文件,但由于某种原因,它在不应该的时候逃脱了*元字符。

#!/opt/anaconda3/bin/perl

# catfastq.pl

# the following 'use' just makes the script print warnings and controles variable scoping (i.e. I have to place 'my' before a new variable declaration)
use warnings;
use strict;

# define array of letters that you will use in the file names
my @letter = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Z');

# store full path to where fastq files are in 'readdir' variable
my $readdir = '/home/data/madzays/finch_data/firstrun';

# store name of output directory in 'outdir' variable
my $outdir = '/home/data/madzays/finch_data/firstrun/combined';

# make output directory if it doesn't exist already
if (!-d $outdir)
{
        if (!mkdir($outdir))
        {
                print STDERR "Couldn't create output directory\n";
                exit 1;
        }
}

# loop through each element of the 'letter' array (note that the element will be stored in the 'lib' variable)
foreach my $lib (@letter)
{
        # the system function executes a command just like if you were to type it in the bash terminal

        # concatenate R1 files
        system("cat ${readdir}/RSFV1${lib}*R1_001.fastq > ${outdir}/RSFV1${lib}_R1.fastq");

        # concatenate R2 files
        system("cat ${readdir}/RSFV1${lib}*R2_001.fastq > ${outdir}/RSFV1${lib}_R2.fastq");
}

exit;

这是输出:

cat:/home/data/madzays/finch_data/firstrun/RSFV1S*R2_001.fastq: No such file or directory 
cat:/home/data/madzays/finch_data/firstrun/RSFV1T*R1_001.fastq: No such file or directory 
cat:/home/data/madzays/finch_data/firstrun/RSFV1T*R2_001.fastq: No such file or directory 
cat:/home/data/madzays/finch_data/firstrun/RSFV1U*R1_001.fastq: No such file or directory 
cat:/home/data/madzays/finch_data/firstrun/RSFV1U*R2_001.fastq: No such file or directory

文件在那里(例如)

RSFV1S_S37_L005_R1.fastq  RSFV1S_S37_L005_R2.fastq  RSFV1S_S37_L006_R1.fastq  RSFV1S_S37_L006_R2.fastq  RSFV1S_S37_L007_R1.fastq  RSFV1S_S37_L007_R2.fastq

enter image description here 什么想法可能是错的?

1 个答案:

答案 0 :(得分:4)

Perl没有逃避*。问题是sh找不到任何匹配项,只有在找到匹配项时才展开*

$ echo fi*le
fi*le

$ touch file

$ echo fi*le
file

问题是您正在使用

system("cat ${readdir}/RSFV1${lib}*R1_001.fastq > ${outdir}/RSFV1${lib}_R1.fastq");

system("cat ${readdir}/RSFV1${lib}*R2_001.fastq > ${outdir}/RSFV1${lib}_R2.fastq");

何时应该使用

system("cat ${readdir}/RSFV1${lib}*001_R1.fastq > ${outdir}/RSFV1${lib}_R1.fastq");

system("cat ${readdir}/RSFV1${lib}*001_R2.fastq > ${outdir}/RSFV1${lib}_R2.fastq");