- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想使用 PowerShell 命令行在 AWK/gawk 中打印字符串文字(具体程序不重要)。但是,我想我误解了沿线某处的引号规则——PowerShell 显然删除了本地命令单引号内的双引号,但在将它们传递给命令行开关时却没有。
这在 Bash 中有效:
bash$ awk 'BEGIN {print "hello"}'
hello <-- GOOD
这在 PowerShell 中有效——但重要的是我不知道为什么需要转义:
PS> awk 'BEGIN {print \"hello\"}'
hello <-- GOOD
这在 PowerShell 中不打印任何内容:
PS> awk 'BEGIN {print "hello"}'
<-- NOTHING IS BAD
如果这真的是在 PowerShell 中执行此操作的唯一方法,那么我想了解解释原因的引用规则链。根据 About Quoting Rules 处的 PowerShell 引用规则,这应该不是必需的。
开始解决
下面由 Duncan 提供的妙语是,您应该将此函数添加到您的 PowerShell 配置文件中:
filter Run-Native($command) { $_ | & $command ($args -replace'(\\*)"','$1$1\"') }
或者专门针对 AWK:
filter awk { $_ | gawk.exe ($args -replace'(\\*)"','$1$1\"') }
结束解决方案
引号已正确传递给 PowerShell 的回显:
PS> echo '"hello"'
"hello" <-- GOOD
但是当调用外部“本地”程序时,引号消失了:
PS> c:\cygwin\bin\echo.exe '"hello"'
hello <-- BAD, POWERSHELL REMOVED THE QUOTES
这是一个更简洁的示例,以防您担心 Cygwin 可能与此有关:
echo @"
>>> // program guaranteed not to interfere with command line parsing
>>> public class Program
>>> {
>>> public static void Main(string[] args)
>>> {
>>> System.Console.WriteLine(args[0]);
>>> }
>>> }
>>> "@ > Program.cs
csc.exe Program.cs
.\Program.exe '"hello"'
hello <-- BAD, POWERSHELL REMOVED THE QUOTES
传递给 cmd 的已弃用示例,它自己进行解析(请参阅下面 Etan 的评论):
PS> cmd /c 'echo "hello"'
"hello" <-- GOOD
传递给 Bash 的已弃用示例,它自己进行解析(请参阅下面 Etan 的评论):
PS> bash -c 'echo "hello"'
hello <-- BAD, WHERE DID THE QUOTES GO
任何解决方案、更优雅的解决方法或解释?
最佳答案
这里的问题是 Windows 标准 C 运行时在解析命令行时从参数中去除未转义的双引号。 PowerShell 通过在参数周围放置双引号将参数传递给 native 命令,但它不会对参数中包含的任何双引号进行转义。
这是一个测试程序,它打印出使用 C stdlib、来自 Windows 的“原始”命令行和 Windows 命令行处理(其行为似乎与 stdlib 相同)给出的参数:
C:\Temp> type t.c
#include <stdio.h>
#include <windows.h>
#include <ShellAPI.h>
int main(int argc,char **argv){
int i;
for(i=0; i < argc; i++) {
printf("Arg[%d]: %s\n", i, argv[i]);
}
LPWSTR *szArglist;
LPWSTR cmdLine = GetCommandLineW();
wprintf(L"Command Line: %s\n", cmdLine);
int nArgs;
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
if( NULL == szArglist )
{
wprintf(L"CommandLineToArgvW failed\n");
return 0;
}
else for( i=0; i<nArgs; i++) printf("%d: %ws\n", i, szArglist[i]);
// Free memory allocated for CommandLineToArgvW arguments.
LocalFree(szArglist);
return 0;
}
C:\Temp>cl t.c "C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x86\shell32.lib"
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
t.c
Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation. All rights reserved.
/out:t.exe
t.obj
"C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x86\shell32.lib"
在 cmd
中运行它,我们可以看到所有未转义的引号都被删除,并且当存在偶数个未转义的引号时,空格仅分隔参数:
C:\Temp>t "a"b" "\"escaped\""
Arg[0]: t
Arg[1]: ab "escaped"
Command Line: t "a"b" "\"escaped\""
0: t
1: ab "escaped"
C:\Temp>t "a"b c"d e"
Arg[0]: t
Arg[1]: ab
Arg[2]: cd e
Command Line: t "a"b c"d e"
0: t
1: ab
2: cd e
PowerShell 的行为有点不同:
C:\Temp>powershell
Windows PowerShell
Copyright (C) 2012 Microsoft Corporation. All rights reserved.
C:\Temp> .\t 'a"b'
Arg[0]: C:\Temp\t.exe
Arg[1]: ab
Command Line: "C:\Temp\t.exe" a"b
0: C:\Temp\t.exe
1: ab
C:\Temp> $a = "string with `"double quotes`""
C:\Temp> $a
string with "double quotes"
C:\Temp> .\t $a nospaces
Arg[0]: C:\Temp\t.exe
Arg[1]: string with double
Arg[2]: quotes
Arg[3]: nospaces
Command Line: "C:\Temp\t.exe" "string with "double quotes"" nospaces
0: C:\Temp\t.exe
1: string with double
2: quotes
3: nospaces
在 PowerShell 中,任何包含空格的参数都用双引号括起来。即使没有任何空格,命令本身也会得到引号。其他参数即使包含双引号等标点符号也不会被引用,而且我认为这是一个错误PowerShell 不会转义出现在参数中的任何双引号。
如果您想知道(我是),PowerShell 甚至懒得引用包含换行符的参数,但参数处理也不会将换行符视为空格:
C:\Temp> $a = @"
>> a
>> b
>> "@
>>
C:\Temp> .\t $a
Arg[0]: C:\Temp\t.exe
Arg[1]: a
b
Command Line: "C:\Temp\t.exe" a
b
0: C:\Temp\t.exe
1: a
b
因为 PowerShell 不会为您转义引号,所以唯一的选择似乎是您自己做:
C:\Temp> .\t 'BEGIN {print "hello"}'.replace('"','\"')
Arg[0]: C:\Temp\t.exe
Arg[1]: BEGIN {print "hello"}
Command Line: "C:\Temp\t.exe" "BEGIN {print \"hello\"}"
0: C:\Temp\t.exe
1: BEGIN {print "hello"}
为避免每次都这样做,您可以定义一个简单的函数:
C:\Temp> function run-native($command) { & $command $args.replace('\','\\').replace('"','\"') }
C:\Temp> run-native .\t 'BEGIN {print "hello"}' 'And "another"'
Arg[0]: C:\Temp\t.exe
Arg[1]: BEGIN {print "hello"}
Arg[2]: And "another"
Command Line: "C:\Temp\t.exe" "BEGIN {print \"hello\"}" "And \"another\""
0: C:\Temp\t.exe
1: BEGIN {print "hello"}
2: And "another"
注意您必须转义反斜杠和双引号,否则这不起作用(这不起作用,请参阅下面的进一步编辑):
C:\Temp> run-native .\t 'BEGIN {print "hello"}' 'And \"another\"'
Arg[0]: C:\Temp\t.exe
Arg[1]: BEGIN {print "hello"}
Arg[2]: And \"another\"
Command Line: "C:\Temp\t.exe" "B EGIN {print \"hello\"}" "And \\\"another\\\""
0: C:\Temp\t.exe
1: BEGIN {print "hello"}
2: And \"another\"
另一个编辑:Microsoft 世界中的反斜杠和引号处理比我意识到的还要奇怪。最终我不得不去阅读 C stdlib 源代码以了解他们如何解释反斜杠和引号:
/* Rules: 2N backslashes + " ==> N backslashes and begin/end quote
2N+1 backslashes + " ==> N backslashes + literal "
N backslashes ==> N backslashes */
所以这意味着 run-native
应该是:
function run-native($command) { & $command ($args -replace'(\\*)"','$1$1\"') }
所有反斜杠和引号都将在命令行处理后继续存在。或者如果你想运行一个特定的命令:
filter awk() { $_ | awk.exe ($args -replace'(\\*)"','$1$1\"') }
(根据@jhclark 的评论更新:它需要是一个过滤器才能允许管道输入标准输入。)
关于windows - 如何将文字双引号从 PowerShell 传递到 native 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21314633/
由于 typescript 现在支持条件类型,我决定进行一些元编程,为 VSCODE intellisense 添加更多 flavor 。但是,虽然使用 A extends B 可以轻松分离其他类型,
我有一个 php var,它在回显时将 JS 函数写入页面的源代码。该函数循环遍历 CSV,因此其中包含以下行: $str="var lines = data.split('\n');"; 目前,当回
我有以下代码: name= "a"; value="b" $.post("ajax.php", {name:value}).... 然而 ajax.php 将收到 name=b 而不是 a=b。 我如
以下情况: var myVehicle = { brand: 'Tesla' }; var isMoving = Symbol(); var currentStatus = Symbol(); myV
我正在为 Python 翻译器编写 JavaScript,“\8”和“\9”给我带来了很多问题。根据文档,像“\8”或“\9”这样的东西是非法的,因为它们不是有效的八进制转义符。 Esprima pa
我有一个简单的 LinearLayout。当我将 android:background 添加到 LinearLayout 时,TextView 不再可见。 我不明白什么?
在尝试获得某种效果时遇到困难: 我有一张图片。我想将鼠标悬停在它上面。它应该变黑并弹出一些文本。 例子^ 执行此操作最简单/最简单的设置是什么?最好只有 HTML 和 CSS 注意:该元素需要在 CS
考虑以下代码: #include void f(const char * str) { str = "java"; } void main (int argc, char * argv[])
我想在 MySQL 中插入一条具有非 ASCII Unicode 字符的记录,但我在一个不允许我轻松键入非 ASCII 字符的终端上。如何在 MySQL 的 SQL 语法中转义 Unicode 文字?
Perl脚本batchReplace.pl可以用来批量替换文件中的文字/代码。可在指定目录中查找指定类型的文件,并递归检查子目录;在输出文件时复制输入文件的目录结构。 [附件]Win32应用程序b
我知道从 flutter 应用程序共享的官方包。 https://pub.dartlang.org/packages/share 共享文本和 url 很容易,但我想共享来自服务器的图像意味着它是 UR
在 Qt 文档中,它说 qreal是一个 Typedef for double unless Qt is configured with the -qreal float option. 这基本上意味
我想让 idris 证明 testMult : mult 3 3 = 9有人居住。 不幸的是,这是键入为 mult (fromInteger 3) (fromInteger 3) = fromInte
如您所知,Apple 为 NSNumber、NSDictionary、NSArray 等类提供了 @literals,因此我们可以通过这种方式创建对象,例如 NSArray *array = @[ob
有没有办法在类型上定义公共(public)文字(C# 中的公共(public) const)?显然 let 类型中的绑定(bind)必须是私有(private)的,并且 Literal 属性不能应用于
为什么下面的语法不能识别 bool 值? 我已经将其与 Java 和 GraphQL 的语法进行了比较,但不明白为什么它不起作用。 给定以下语法,解析如下: foo = null // foo = v
当我需要一个文字来测试一个带有序列的函数时,我注意到我自己的习惯比列表更频繁地使用向量。 IE。 : (map inc [1 2 3]) 但不是: (map inc (list 1 2 3)) 虽然它
是否可以在sqlite中制作类似的东西? FOREIGN KEY(TypeCode, 'ARawValue', IdServeur) REFERENCES OTHERTABLE(TypeCode, T
在我这学期的系统软件类(class)中,我们正在学习汇编程序和其他系统软件。在阅读整个类(class)时,我遇到了LITERALS主题。 对文字和立即操作数进行了比较,发现它们之间的唯一区别是文字不被
我正在使用命令行语法编写一个 gstreamer 管道来发送视频流,并想用它发送数据。我认为可行的一种解决方案是将数据作为字幕文件发送。 我可以使用以下管道通过 rtp 多路复用视频和字幕文件: gs
我是一名优秀的程序员,十分优秀!