Why is Perl system call failing to invoke internal

2019-07-18 06:59发布

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?

2条回答
闹够了就滚
2楼-- · 2019-07-18 07:31

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.

查看更多
闹够了就滚
3楼-- · 2019-07-18 07:42

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`"
…
查看更多
登录 后发表回答