我想使用Powershell命令行在awk/gawk中打印字符串文字(具体程序不重要).但是,我认为我误解了引用规则 - PowerShell显然删除了本机命令的单引号内的双引号,但在将它们传递给命令行开关时却没有.
这适用于bash:
bash$ awk 'BEGIN {print "hello"}'
hello <-- GOOD
Run Code Online (Sandbox Code Playgroud)
这适用于PowerShell - 但重要的是我不知道为什么需要转义:
PS> awk 'BEGIN {print \"hello\"}'
hello <-- GOOD
Run Code Online (Sandbox Code Playgroud)
这在PowerShell中不打印任何内容:
PS> awk 'BEGIN {print "hello"}'
<-- NOTHING IS BAD
Run Code Online (Sandbox Code Playgroud)
如果这确实是在PowerShell中执行此操作的唯一方法,那么我想了解解释原因的引用规则链.根据http://technet.microsoft.com/en-us/library/hh847740.aspx上的PowerShell引用规则,这不是必需的.
开始解决方案
下面的Duncan提供的妙语是,您应该将此功能添加到您的powershell配置文件中:
filter Run-Native($command) { $_ | & $command ($args -replace'(\\*)"','$1$1\"') }
Run Code Online (Sandbox Code Playgroud)
或者特别是对于awk:
filter awk { $_ | gawk.exe ($args -replace'(\\*)"','$1$1\"') }
Run Code Online (Sandbox Code Playgroud)
结束解决方案
引号正确传递给PowerShell的echo:
PS> echo '"hello"'
"hello" <-- GOOD
Run Code Online (Sandbox Code Playgroud)
但是当呼唤外部"本地"程序时,引号会消失:
PS> c:\cygwin\bin\echo.exe '"hello"'
hello <-- BAD, POWERSHELL REMOVED THE QUOTES
Run Code Online (Sandbox Code Playgroud)
这是一个更清晰的例子,如果您担心Cygwin可能与此有关: …
我有一个在Unix机器上很好地构建的项目(http://www.github.com/jhclark/ducttape).
但是,使用SBT 0.11.2(和其他几个版本的SBT),它不会在我的Mac(OSX 10.5)上构建.我收到以下神秘错误消息:
$ ~/bin/sbt compile (master*? 20:11)
[info] Loading project definition from /Users/jon/Documents/workspace- scala/ducttape/project
[info] Set current project to ducttape (in build file:/Users/jon/Documents/workspace-scala/ducttape/)
[info] Compiling 104 Scala sources to /Users/jon/Documents/workspace-scala/ducttape/target/scala-2.9.2/classes...
[error] error while loading <root>, error in opening zip file
[error] {file:/Users/jon/Documents/workspace-scala/ducttape/}default-024416/compile:compile: scala.tools.nsc.MissingRequirementError: object scala not found.
[error] Total time: 2 s, completed May 27, 2012 8:12:09 PM
Run Code Online (Sandbox Code Playgroud)
即使我彻底清理了以下内容后,也会发生这种情况
sbt clean clean-files
rm -rf ~/.ivy2 ~/.m2 ~/.sbt
Run Code Online (Sandbox Code Playgroud)
我怀疑真正的错误发生在Maven2中,SBT用于依赖管理(另请参阅Maven:运行maven时打开zip文件时出错).
但是,几天后我才难过.有任何想法吗?