在 Powershell Invoke-Expression 中转义分散变量

发布于 2024-10-31 03:31:06 字数 846 浏览 3 评论 0原文

我正在尝试从 Powershell 调用另一个应用程序(超越比较),该应用程序在典型的命令行中需要 @:

C:\deploy>bcompare @static.diff

我找到了 Powershell 的 Invoke-Expression,但是当我尝试以下操作时,它给了我一个错误:

PS C:\deploy>Invoke-Expression "bcompare @static.diff"
Invoke-Expression : Cannot expand the splatted variable '@static'. Splatted variables
cannot be used as part of a property or array expression. Assign the result of the 
expression to a temporary variable then splat the temporary variable instead.
At line:1 char:18
    + Invoke-Expression <<<<  "bcompare @static.diff"
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : NoPropertiesInSplatting,Microsoft.PowerShell.Comands.InvokeExpressionCommand

我不能让@在这里正确转义。我尝试过`、@@,将命令的一部分放入临时变量中,但没有一个成功。

I'm trying to invoke another application (Beyond Compare) from Powershell which requires an @ in the typical command-line:

C:\deploy>bcompare @static.diff

I've found Powershell's Invoke-Expression, but when I try the following it gives me an error:

PS C:\deploy>Invoke-Expression "bcompare @static.diff"
Invoke-Expression : Cannot expand the splatted variable '@static'. Splatted variables
cannot be used as part of a property or array expression. Assign the result of the 
expression to a temporary variable then splat the temporary variable instead.
At line:1 char:18
    + Invoke-Expression <<<<  "bcompare @static.diff"
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : NoPropertiesInSplatting,Microsoft.PowerShell.Comands.InvokeExpressionCommand

I can't get the @ to escape properly here. I've tried the `, @@, putting parts of the command in a temporary variable, but none of them did the trick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

看海 2024-11-07 03:31:06
bcompare '@static.diff'

如果有疑问,请将其放入字符串中:-)

PS Home:\> args '@static.diff'
argv[0] = "C:\Users\Joey\Batches\args.cmd"
argv[1] = @static.diff
bcompare '@static.diff'

If in doubt, put it into a string :-)

PS Home:\> args '@static.diff'
argv[0] = "C:\Users\Joey\Batches\args.cmd"
argv[1] = @static.diff
金橙橙 2024-11-07 03:31:06

你需要双重逃避,因为你正在经历两个层次的解释。只有一个 ` 不起作用,因为它在字符串创建过程中被解析。

Invoke-Expression "bcompare ``@static.diff"

或者正如乔伊所说。

Invoke-Expression "bcompare '@static.diff'"

You need to double escape, because you are going through two levels of interpretation. Only one ` will not work because it get parsed during the string creation.

Invoke-Expression "bcompare ``@static.diff"

Or as Joey said.

Invoke-Expression "bcompare '@static.diff'"
流年已逝 2024-11-07 03:31:06

当我遇到同样的问题时,我使用反引号来按字面解释@符号。我也想使用双引号进行变量处理:

<块引用>

调用表达式“& bcompare `@$compareCommands $file1 $file2”

When I ran into the same problem, I used a backtick to make the @-sign interpreted literally. I wanted to use double-quotes for variable handling as well:

Invoke-Expression "& bcompare `@$compareCommands $file1 $file2"

望她远 2024-11-07 03:31:06

我在 Windows 命令行上的 npm install 上遇到错误

splatting 运算符“@”不能用于引用表达式中的变量。

npm install @neville.dabreo/greetingbot

The splatting operator '@' cannot be used to reference variables in an expression.

但单引号解决了这个问题

npm install '@neville.dabreo/greetingbot'

注意 - NPM 官方网站没有提到单引号。

I was getting an error on npm install on the windows command line

The splatting operator '@' cannot be used to reference variables in an expression.

npm install @neville.dabreo/greetingbot

The splatting operator '@' cannot be used to reference variables in an expression.

But single quotes resolved the problem

npm install '@neville.dabreo/greetingbot'

NOTE - Official NPM site doesn't mention a single quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文