Search code examples
perlpathcygwin

Cannot execute system command in cygwin


I have the following simple perl script that I cannot execute in cygwin:

#!/usr/bin/perl

use strict;


system("../cat.exe < a.txt > b.txt");

When I run it, the script tells me:

./my_test.pl
'..' is not recognized as an internal or external command,
operable program or batch file.

However I can run the command in the cygwin shell:

$ ../cat.exe < a.txt > b.txt

$ ../cat.exe b.txt
hello

The executable cat.exe exists in the directory above and a.txt in the current working directory.

My version of perl: $ perl -v

This is perl, v5.8.8 built for MSWin32-x86-multi-thread (with 12 registered patches, see perl -V for more detail)


Solution

  • You're using a perl built for Windows (ActiveState? Strawberry?), not the Cygwin version. It invokes cmd.exe for system(), which thinks that .. is the command and / introduces an option.

    Try changing the the system() call to:

    system("..\\cat.exe < a.txt > b.txt");
    

    But you should normally be using the Cygwin version of perl when running a script from bash.

    What is the output of the following commands?

    echo "$PATH"
    type -a perl
    /usr/bin/perl -v
    

    From what we've seen so far, it looks like you've installed some Windows-specific Perl with its perl.exe in your Cygwin /usr/bin directory. If so, then (a) uninstall it (you can reinstall it elsewhere if you like), and (b) re-install the "perl" package via Cygwin's setup.exe.

    (And add use warnings; after use strict; in your Perl scripts. This isn't related to your problem, but it's good practice.)