gpt4 book ai didi

powershell - Powershell不会将变量扩展为参数

转载 作者:行者123 更新时间:2023-12-03 00:56:23 25 4
gpt4 key购买 nike

我在Powershell脚本中遵循以下命令:

$boostPath = "D:/lib/boost/1.62.0-vs2013-x64"
cmake -S $cmakeRootPath -B $buildPath -G $cmakeGenerator -DBOOST_ROOT=$boostPath
这样,变量 $boostPath不会扩展。我可以在 FindBoost日志中看到
-- [ C:/Program Files/CMake/share/cmake-3.18/Modules/FindBoost.cmake:1528 ] BOOST_ROOT = "$boostPath"
相反,如果在脚本中我写
cmake -S $cmakeRootPath -B $buildPath -G $cmakeGenerator -DBOOST_ROOT="D:/lib/boost/1.62.0-vs2013-x64"
变量设置正确,cmake起作用。如何使用变量正确给出参数?

最佳答案

Mathias R. Jessen提供了注释中的关键指针:
PowerShell将诸如-DBOOST_ROOT=$boostPath之类的标记解析为文字-$boostPath变量引用无法识别,因此无法展开(由其值替换)。
this GitHub issue中总结了PowerShell带有无引号的复合 token 的令人惊讶的行为。
以下通常应该起作用,并且如果$boostPath不包含空格,则肯定可以起作用:

cmake -S $cmakeRootPath -B $buildPath -G $cmakeGenerator -DBOOST_ROOT="$boostPath"
可变引用的双引号强制其扩展。
注意:
  • 由于PowerShell仅在需要时(请参阅this answer)并且在路径中不包含空格的情况下在后台执行重新报价(保留双引号(可能从单引号转换)),因此在命令行上看到的cmake是逐字记录的-DBOOST_ROOT=D:/lib/boost/1.62.0-vs2013-x64-无引号。
  • 如果$boostPath确实包含空格(例如$boostPath = "D:/lib 1/boost/1.62.0-vs2013-x64"),PowerShell会将整个参数双引号引起来,以便cmake在命令行上看到的内容是逐字的"-DBOOST_ROOT=D:/lib 1/boost/1.62.0-vs2013-x64"
  • 如果Windows [1]上的cmake确实真正要求"..."仅在$boostPath值附近(不幸的是,某些CLI需要此,则使用解决方法(为简洁起见,省略了一个参数):

  • cmake -S $cmakeRootPath -B $buildPath -DBOOST_ROOT="`"$boostPath`""
    警告:如果应将PowerShell传递给外部程序(请参阅 this answer)的参数的(重新)引用弄乱(请参见 this GitHub issue),则此解决方法将被打破。

    [1]仅在Windows上,(控制台)程序才需要将整个命令行解析为单独的参数。在类似Unix的平台上,它们明智地接收了一系列逐字标记。

    关于powershell - Powershell不会将变量扩展为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62776512/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com