如何保护批处理文件中的引号?

时间:2022-01-19 02:23:44

I want to wrap a Perl one-liner in a batch file. For a (trivial) example, in a Unix shell, I could quote up a command like this:

我想在批处理文件中包装一个Perl单行程序。对于(平凡的)示例,在Unix shell中,我可以引用如下命令:

perl -e 'print localtime() . "\n"'

But DOS chokes on that with this helpful error message:

但DOS使用这个有用的错误消息扼杀了它:

Can't find string terminator "'" anywhere before EOF at -e line 1.

在-e第1行的EOF之前的任何地方都找不到字符串终止符“'”。

What's the best way to do this within a .bat file?

在.bat文件中执行此操作的最佳方法是什么?

5 个解决方案

#1


13  

For Perl stuff on Windows, I try to use the generalized quoting as much as possible so I don't get leaning toothpick syndrome. I save the quotes for the stuff that DOS needs:

对于Windows上的Perl内容,我尝试尽可能多地使用广义引用,这样我就不会得到倾斜的牙签综合症。我保存了DOS所需内容的引号:

perl -e "print scalar localtime() . qq(\n)"

If you just need a newline at the end of the print, you can let the -l switch do that for you:

如果您只需要在打印结束时使用换行符,可以让-l开关为您执行此操作:

perl -le "print scalar localtime()"

For other cool things you can do with switches, see the perlrun documentation.

有关交换机可以执行的其他很酷的事情,请参阅perlrun文档。

#2


8  

In Windows' "DOS prompt" (cmd.exe) you need to use double quotes not single quotes. For inside the quoted Perl code, Perl gives you a lot of options. Three are:

在Windows的“DOS提示符”(cmd.exe)中,您需要使用双引号而不是单引号。对于引用的Perl代码,Perl为您提供了很多选择。三是:

perl -e "print localtime() . qq(\n)"
perl -e "print localtime() . $/"
perl -le "print ''.localtime()"

If you have Perl 5.10 or newer:

如果您有Perl 5.10或更新版本:

perl -E "say scalar localtime()"

Thanks to J.F. Sebastian's comment.

感谢J.F. Sebastian的评论。

#3


2  

For general batch files under Windows NT+, the ^ character escapes lots of things (<>|&), but for quotes, doubling them works wonders:

对于Windows NT +下的常规批处理文件,^字符会逃避很多事情(<> |&),但对于引号,将它们加倍会产生奇迹:

C:\>perl -e "print localtime() . ""\n"""
Thu Oct  2 09:17:32 2008

#4


1  

First, any answer you get to this is command-specific, because the DOS shell doesn't parse the command-line like a uniq one does; it passes the entire unparsed string to the command, which does any splitting. That said, if using /subsystem:console the C runtime provides splitting before calling main(), and most commands use this.

首先,你得到的任何答案都是特定于命令的,因为DOS shell不像uniq那样解析命令行;它将整个未解析的字符串传递给命令,该命令执行任何拆分。也就是说,如果使用/ subsystem:console,C运行时会在调用main()之前提供拆分,大多数命令都会使用它。

If an application is using this splitting, the way you type a literal double-quote is by doubling it. So you'd do

如果应用程序正在使用此拆分,则键入文字双引号的方式是将其加倍。所以你会这样做

perl -e "print localtime() . ""\n"""

#5


1  

In DOS, you use the "" around your Perl command. The DOS shell doesn't do single quotes like the normal Unix shell:

在DOS中,您使用Perl命令周围的“”。 DOS shell不像普通的Unix shell那样做单引号:

perl -e "print localtime();"

#1


13  

For Perl stuff on Windows, I try to use the generalized quoting as much as possible so I don't get leaning toothpick syndrome. I save the quotes for the stuff that DOS needs:

对于Windows上的Perl内容,我尝试尽可能多地使用广义引用,这样我就不会得到倾斜的牙签综合症。我保存了DOS所需内容的引号:

perl -e "print scalar localtime() . qq(\n)"

If you just need a newline at the end of the print, you can let the -l switch do that for you:

如果您只需要在打印结束时使用换行符,可以让-l开关为您执行此操作:

perl -le "print scalar localtime()"

For other cool things you can do with switches, see the perlrun documentation.

有关交换机可以执行的其他很酷的事情,请参阅perlrun文档。

#2


8  

In Windows' "DOS prompt" (cmd.exe) you need to use double quotes not single quotes. For inside the quoted Perl code, Perl gives you a lot of options. Three are:

在Windows的“DOS提示符”(cmd.exe)中,您需要使用双引号而不是单引号。对于引用的Perl代码,Perl为您提供了很多选择。三是:

perl -e "print localtime() . qq(\n)"
perl -e "print localtime() . $/"
perl -le "print ''.localtime()"

If you have Perl 5.10 or newer:

如果您有Perl 5.10或更新版本:

perl -E "say scalar localtime()"

Thanks to J.F. Sebastian's comment.

感谢J.F. Sebastian的评论。

#3


2  

For general batch files under Windows NT+, the ^ character escapes lots of things (<>|&), but for quotes, doubling them works wonders:

对于Windows NT +下的常规批处理文件,^字符会逃避很多事情(<> |&),但对于引号,将它们加倍会产生奇迹:

C:\>perl -e "print localtime() . ""\n"""
Thu Oct  2 09:17:32 2008

#4


1  

First, any answer you get to this is command-specific, because the DOS shell doesn't parse the command-line like a uniq one does; it passes the entire unparsed string to the command, which does any splitting. That said, if using /subsystem:console the C runtime provides splitting before calling main(), and most commands use this.

首先,你得到的任何答案都是特定于命令的,因为DOS shell不像uniq那样解析命令行;它将整个未解析的字符串传递给命令,该命令执行任何拆分。也就是说,如果使用/ subsystem:console,C运行时会在调用main()之前提供拆分,大多数命令都会使用它。

If an application is using this splitting, the way you type a literal double-quote is by doubling it. So you'd do

如果应用程序正在使用此拆分,则键入文字双引号的方式是将其加倍。所以你会这样做

perl -e "print localtime() . ""\n"""

#5


1  

In DOS, you use the "" around your Perl command. The DOS shell doesn't do single quotes like the normal Unix shell:

在DOS中,您使用Perl命令周围的“”。 DOS shell不像普通的Unix shell那样做单引号:

perl -e "print localtime();"