Bash Perl system()调用转义元字符

Bash Perl system()调用转义元字符,bash,shell,perl,Bash,Shell,Perl,我的学生编写了一个简单的程序,调用系统来cat一些文件,但出于某种原因,它在不应该的时候逃逸了*元字符 #!/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

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

#!/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
有什么想法可能是错误的吗?

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");
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");

由于某种原因,它会在不应该的时候逃逸*元字符,但不会。当没有匹配项时,sh不会展开*。确切地说,请查看此链接:是您的shell执行解释通配符的魔法,而不是cat命令的cat命令。它是完全透明的,它只接收来自其父进程shell的参数列表!!!在您的例子中,您直接从perl进程调用cat命令!同时检查:@Allan搞错了。你涉及到了炮弹。如果没有匹配项,它就不会展开。这可以用sh-c'echo nonexistent*'@Allan,不,我是说systemEXPR是system'/bin/sh'的缩写,'-c',EXPR看到,如果EXPR不包含除空格以外的shell元字符,有一个优化可以避免shell,但这里不是这样。您的shell不同意,我比您更信任它。您可能在错误的目录中查找,或者可能在名称中没有看到字符。或者它们可能在那里,但是权限问题阻止它们被看到,尽管我认为这会给出不同的错误消息。当没有匹配项时,sh不会展开*。确切地说,请查看此链接:是您的shell执行解释通配符的魔法,而不是cat命令的cat命令。它是完全透明的,它只接收来自其父进程shell的参数列表!!!在您的例子中,您直接从perl进程调用cat命令!同时检查:@Allan搞错了。你涉及到了炮弹。如果没有匹配项,它就不会展开。这可以用sh-c'echo nonexistent*'@Allan,不,我是说systemEXPR是system'/bin/sh'的缩写,'-c',EXPR看到,如果EXPR不包含除空格以外的shell元字符,有一个优化可以避免shell,但这里不是这样。您的shell不同意,我比您更信任它。您可能在错误的目录中查找,或者可能在名称中没有看到字符。或者它们在那里,但是权限问题阻止它们被看到,尽管我认为这会给出不同的错误消息。谢谢!当你说你的外壳不同意时,你是对的,我比你更信任它;非常感谢。当你说你的外壳不同意时,你是对的,我比你更信任它;