Madza Farias-Virgens
Madza Farias-Virgens

Reputation: 1061

Perl system() call escapes metacharacter

My student wrote this simple program that calls system to cat some files, but for some reason it escapes the * metacharacter when it shouldn't.

#!/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;

This is the output:

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

Files are there (for example)

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 Any ideas what could be wrong?

Upvotes: 1

Views: 228

Answers (1)

ikegami
ikegami

Reputation: 385847

Perl is not escaping the *. The issue is that sh doesn't find any matches, and it only expands * when it finds a match.

$ echo fi*le
fi*le

$ touch file

$ echo fi*le
file

The problem is that you are using

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

when you should be using

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

Upvotes: 5

Related Questions