2

On one of our Windows XP machines, Perl system commands such as dir /b generate an error message such as: /b: no such file or directory. In other words, the switch is being interpreted as a filename.

This occurs whether I use backticks, open() or system(). I even tried passing in the switch as a separate arg to system(). Naturally, I have confirmed that the call works correctly on the DOS command line or batch script.

Has anybody else encountered this?

UhClem
  • 129
  • 3
  • 10
  • Have you installed the Perl interpreter for Windows on this machine? Windows does not natively run Perl scripts. Sorry basic question but it seems strange. If you have installed this could you please try and escape the '/'? Been a while since I worked with Perl on Windows – Namphibian Apr 12 '12 at 07:35
  • Perl is just escaping the special characters that should be standard behaviour if my memory serves me correctly. – Namphibian Apr 12 '12 at 07:40
  • Namphibian, yes, ActivePerl is installed on this machine, and it seems to work fine with other aspects of Perl. I tried reinstalling Perl on this computer to make sure it matched our other computers (which do not have this problem). BTW, all are running on the same OS (XP SP3). – UhClem Apr 12 '12 at 07:47
  • 3
    Edit your question by adding the smallest snippet of code you can provide that replicates the problem. Show us what you're doing. – DavidO Apr 12 '12 at 07:48
  • I should also mention that if I specify a folder name to dir, the Perl system command echoes it back with backslashes before special characters like colons, spaces and, yes, backslashes. So system("dir C:\temp\my folder") returns "C\:\\temp\\my\ folder" (followed by the folder contents). It's almost as if the command line were being parsed as a UNIX command line, even though the command itself is understood to be DOS. (Note: I used single backslashes in the above dir example for clarity. In an actual Perl program, I would have to escape each backslash with another backslash.) – UhClem Apr 12 '12 at 07:53
  • Does the same command (in system call) work properly at DOS? (without perl script) –  Apr 12 '12 at 07:59
  • David, my snippet of code is simply: system("dir /b"). Or system("dir", "/b"). Or print `dir /b`. – UhClem Apr 12 '12 at 08:00
  • In the above comment, the last example was supposed to be print dir /b . I guess stackoverflow converts these things to code snippets. (Sorry, I'm new at this.) – UhClem Apr 12 '12 at 08:08
  • @user1225101 So on your system, the following command produces the error? `perl -e "system(qq{dir /b})"` – DavidO Apr 12 '12 at 08:24
  • David O - yes, it produces the same error. – UhClem Apr 13 '12 at 07:47

2 Answers2

5

You probably have Cygwin installed and dir.exe is in your path which is not the cmd.exe built-in but an alias to ls.

C:\> which dir
/usr/bin/dir

C:\> c:\opt\cygwin\bin\dir.exe --version
dir (GNU coreutils) 8.15
Packaged by Cygwin (8.15-1)
…

C:\> dir /b
…

C:\> perl -e "print `dir /b`"
dir: cannot access /b: No such file or directory

C:\> perl -e "print `cmd /c dir /b`"
…
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • Sinan, this is it! Thank you! As I implied in my previous comment, I could "smell" UNIX! :) – UhClem Apr 13 '12 at 07:04
  • The problem is solved (thanks again!), but there are two things I don't get: 1. Why would Perl be seeing a different path than DOS? (Or rather, why would the Perl 'system' command bypass the internal/built-in DOS 'dir' command and use the path instead?) 2. How is it that Cygwin has a presence in the DOS command window? I have Cygwin installed on my own PC, but I have to specifically launch the bash shell to use it. But on this particular PC, I can bring up a normal DOS command window and type in UNIX commands such as 'ls' and 'which'. I have a sense that these two questions are related. – UhClem Apr 13 '12 at 07:15
  • Sinan, by "DOS" I meant DOS command window -- a holdover from the dinosaur age. "Windows Command Line Interface" is probably more accurate. Good point about %PATH%. I had noticed CYGWIN\bin in the path and had assumed that was standard for Cygwin installs. But I checked my own PC (which also has Cygwin installed), and there's no CYGWIN in %PATH%. – UhClem Apr 20 '12 at 20:17
  • By the way, system("dir /b") works correctly on dozens of lab machines. The problem was limited to this one (probably the only one with CYGWIN in the path). – UhClem Apr 20 '12 at 20:18
  • It may work in 99 out of 100 cases, but it is safer not to leave it to chance and use `cmd /c dir /b` when you want to unambiguously refer to the `cmd.exe` builtin. – Sinan Ünür Apr 20 '12 at 21:19
1

Unverified:

dir is a command interpreter built-in command. Run the command interpreter with a /c or /k switch instead, followed by the command you want to execute.

daxim
  • 39,270
  • 4
  • 65
  • 132