简体   繁体   English

如何将单引号(')传递到批处理脚本中?

[英]How to pass a single quote (') into a batch script?

Hello and thanks for the help. 您好,感谢您的帮助。

I'm having difficulty escaping the single quote character (') in a Windows Batch script. 我很难在Windows Batch脚本中转义单引号(')。 I've tried ^', \\', ''', '\\'', and so on. 我已经尝试过^',\\',''','\\''等。 Online searches haven't solved it either. 在线搜索也没有解决它。 I'm stumped. 我很沮丧

This script uses cgywin64 gawk to format a list of folders: 此脚本使用cgywin64 gawk格式化文件夹列表:

@echo off
du -sk ./*  | sort -nr | head -40 | gawk '{printf("%%^'14d kB %%s %%s %%s %%s %%s %%s %%s %%s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9)}'

When run, it gives me the following errors: 运行时,出现以下错误:

gawk: cmd. line:1: {printf("%^14d
gawk: cmd. line:1:         ^ unterminated string
gawk: cmd. line:1: {printf("%^14d
gawk: cmd. line:1:         ^ syntax error

Without doing any character escaping, the gawk command would look something like: 如果不进行任何字符转义,gawk命令将类似于:

gawk '{printf("%'14d kB %s %s %s %s %s %s %s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9)}'

The format string at the beginning (%'14d) tells gawk to use commas when printing the integer ("123456789" prints as "123,456,789"). 开头的格式字符串(%'14d)告诉gawk在打印整数时使用逗号(“ 123456789”打印为“ 123,456,789”)。 This string has a single quote ('), which I can't figure out how to pass into the batch script without errors. 该字符串有一个单引号('),我无法弄清楚如何正确地将其传递到批处理脚本中。

So how do I pass a single quote (') into a batch script? 那么,如何将单引号(')传递到批处理脚本中?

Any help appreciated. 任何帮助表示赞赏。

The following advice is untested, and is currently based on my limited understanding. 以下建议未经测试,目前基于我的有限理解。

gAwk uses single quotes for quoting in POSIX-compliant, Bourne-style shells of which cmd.exe isn't one. gAwk在POSIX兼容的Bourne风格的shell中使用单引号进行引用,而cmd.exe不是其中的一个。

With cmd.exe those single quotes used for quoting, ' , need to be replaced by double quotes, " . 使用cmd.exe时,那些用于引号'单引号需要用双引号"代替。

The single quote not used for quoting, %'14d , does not require special attention by gAwk because characters within double quotes are protected. gAwk无需特别注意不用于引用%'14d的单引号,因为双引号内的字符受到保护。

In cmd.exe single quotes are not problematic, so this character can remain untouched. 在cmd.exe中,单引号没有问题,因此该字符可以保持不变。

So in cmd.exe you need to replace the surrounding single quotes '{ and }' with double quotes "{ and }" . 因此,在cmd.exe中,您需要将双引号"{}"替换为单引号'{}'

Then you have to escape the internal double quotes to prevent cmd.exe from closing the opening one, "{ , prematurely. 然后,您必须转义内部双引号,以防止cmd.exe过早关闭打开的引号"{

In cmd.exe a double quote is usually escaped by preceding it with a backslash, \\" . 在cmd.exe中,双引号通常通过在反引号前加反斜杠\\"转义。

Your command in cmd.exe would therefore look like this: 因此,您在cmd.exe中的命令将如下所示:

du -sk ./*  | sort -nr | head -40 | gawk "{printf(\"%'14d kB %s %s %s %s %s %s %s %s\n\", $1, $2, $3, $4, $5, $6, $7, $8, $9)}"

The advice above is for cmd.exe; 上面的建议是针对cmd.exe的; however when running from a batch file there is an additional gotcha. 但是,从批处理文件运行时,还有其他陷阱。

All percent characters require doubling! 所有百分比字符都需要加倍! % becomes %% . %变为%%

So given all of the above, remembering that this is completely untested, your command in a batch file would be: 因此,鉴于以上所有情况,请记住这是未经测试的,批处理文件中的命令为:

du -sk ./*  | sort -nr | head -40 | gawk "{printf(\"%%'14d kB %%s %%s %%s %%s %%s %%s %%s %%s\n\", $1, $2, $3, $4, $5, $6, $7, $8, $9)}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将转义双引号传递给批处理文件 - How to pass escaped double quote to batch file 双引号和单引号会在Windows批处理脚本中产生问题以终止进程 - Double and single quote creates issue in Windows batch script to kill process 在 Windows 批处理中 - 如何用转义单引号替换单引号以提供 SQL - In Windows batch - how do I replace single quote with escape single quote to feed SQL 如何将具有多个空格的变量传递给批处理脚本? - How to pass a variable with multiple spaces to a batch script? CMD / windows Batch:如何在命令参数中传递双引号和大于号? - CMD/windows Batch: how to pass a double quote and a larger than sign in a command argument? 当左侧是双引号时如何比较批处理脚本中的字符 - How to compare characters in Batch script when left hand side is double quote 如何使用批处理文件在单个变量中传递多个值 - How to pass multiple values in a single variable using batch file 如何将带点的批处理参数传递给powershell脚本? - How can I pass batch arguments with dots into a powershell script? 如何将Windows批处理脚本变量传递给管道命令 - How to pass a windows batch script variable to a piped command 如何将变量从批处理文件传递到Powershell脚本 - How to pass variable from Batch File to a Powershell Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM