Search code examples
perlsystem

How to handle quotes in Perl when calling system commands?


I'm trying to make a very simple script to call the PDFXchange viewer from a .desktop file. But I'm not able to do this because the bash is seeing things that I'm not aware:

#!/usr/bin/perl

use strict;
use warnings;

my $winepath = `winepath -w -0 "$ARGV[0]"`;

my $cmd=join ' ',
    'wine',
    '\'C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe\'',
    "\'$winepath\'";

print $cmd . "\\n";

exec $cmd;

Output

$ exec_pdfxcv 'pdf with spaces.pdf' 
wine 'C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe' 'Z:\media\FILES\pdf with spaces.pdf'\nsh: 1: Syntax error: Unterminated quoted string

I'm very beginner at Perl, so I run out of ideas very quickly after tried a couple of times with different quotes configurations.


Solution

  • I finally make it work after a hundred of tries.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $winepath = `winepath -w -0 "$ARGV[0]"`;
    
    exec 'wine', q(C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe), "".$winepath."";
    

    I hope help someone!

    If there is anyone interested in the .desktop file:

    [Desktop Entry]
    Name=PDF XChange Viwer
    Comment=View multi-page documents
    Exec=exec_pdfxcv %U
    StartupNotify=true
    Terminal=false
    Type=Application
    StartupWMClass=PDFXCview.exe
    Icon=evince
    Categories=GNOME;GTK;Office;Viewer;
    MimeType=application/pdf;application/x-bzpdf;application/x-gzpdf;application/x-xzpdf;application/x-ext-pdf;application/postscript;application/x-bzpostscript;application/x-gzpostscript;image/x-eps;image/x-bzeps;image/x-gzeps;application/x-ext-ps;application/x-ext-eps;application/x-dvi;application/x-bzdvi;application/x-gzdvi;application/x-ext-dvi;image/vnd.djvu;image/vnd.djvu+multipage;application/x-ext-djv;application/x-ext-djvu;image/tiff;application/x-cbr;application/x-cbz;application/x-cb7;application/x-cbt;application/x-ext-cbr;application/x-ext-cbz;application/vnd.comicbook+zip;application/x-ext-cb7;application/x-ext-cbt;application/oxps;application/vnd.ms-xpsdocument;