当我尝试在 Windows 上运行 Perl one-liner 时,为什么会在 EOF 之前的任何位置找到字符串终止符“'”,在 -e line 1”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7761057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:49:06  来源:igfitidea点击:

Why am I getting "Can't find string terminator "'" anywhere before EOF at -e line 1" when I try to run a Perl one-liner on Windows?

windowsperlcmd

提问by R.D

I am trying to run the following on Windows with 5.14.2

我正在尝试使用 5.14.2 在 Windows 上运行以下内容

C:\Perl>perl -e 'print "Hello World \n"'
Can't find string terminator "'" anywhere before EOF at -e line 1.

What am I missing?

我错过了什么?

回答by cjm

You're missing a decent shell with sane and well-defined quoting rules. On Windows, only the double quote is considered a quote, and the escaping rules are poorly defined and inconsistent. Try:

您缺少一个体面的外壳,它具有合理且定义明确的引用规则。在 Windows 上,只有双引号被认为是引号,并且转义规则定义不明确且不一致。尝试:

perl -e "print qq{Hello World \n}"

I strongly recommend avoiding anything but the very simplest one-liners on Windows. (Another problem with Windows one-liners is that the Windows shell doesn't expand wildcards. If you use *.txton the command line, it'll look for a file named literally *.txt. You'll run into that later.)

我强烈建议避免使用 Windows 上最简单的单行代码以外的任何内容。(Windows one-liners 的另一个问题是 Windows shell 不会扩展通配符。如果您*.txt在命令行上使用,它会查找字面命名的文件*.txt。稍后您会遇到它。)

On Windows, what you typed is equivalent to:

在 Windows 上,您键入的内容相当于:

perl -e "'print" "Hello World \n'"

That is, the code Perl is trying to execute is 'printwith @ARGVcontaining the single string Hello World \n'. (That's not a newline, that's a backslash followed by n).

也就是说,Perl的正试图执行的代码是'print@ARGV含有单个字符串Hello World \n'。(那不是换行符,而是反斜杠后跟n)。

回答by user1735885

On Windows, the quotation marks should be reversed. So, rather than:

在 Windows 上,引号应该颠倒。所以,而不是:

C:\Perl>perl -e 'print "Hello World \n"'

it should be:

它应该是:

C:\Perl>perl -e "print 'Hello World \n'"

(attribution Learning Perl, 6th edition, p. 295)

(归因学习 Perl,第 6 版,第 295 页)

回答by Glenn

I also found this to work. I am using Windows 7 using c:\Windows\system32\cmd.exe

我也发现这有效。我正在使用 Windows 7 使用c:\Windows\system32\cmd.exe

perl -e "$a=2; print(\"$a \n \");"

I'm using the escape slash in my print statement to get the quotes to appear \"

我在我的打印语句中使用转义斜杠来让引号出现 \"

回答by Vetrivel Santhanam

On Windows the following command works:

在 Windows 上,以下命令有效:

perl -e "print \"Hello world\""

perl -e "print \"Hello world\""