gpt4 book ai didi

powershell - 何时可以在我的 Powershell 命令中使用换行符(换行符)来提高可读性

转载 作者:行者123 更新时间:2023-12-03 16:38:02 25 4
gpt4 key购买 nike

我如何知道何时可以在我的 Powershell 脚本中使用换行符/回车符?搜索此答案时的所有搜索结果都指向输出。在这种情况下,我不关心输出。我对格式化 Powershell 脚本以提高可读性的能力更感兴趣。

例如。下面有两个版本的 Powershell 命令行。一种有效,一种无效。在这种情况下,该命令的作用并不重要。关键是我需要知道什么时候可以创建新行,什么时候不允许。

这个命令行的工作原理是一个很长的单行:

& 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\arangoimp.exe' --file 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json' --type json --collection users --progress true --overwrite true --server.username root --server.password password

由于脚本中间有一个新行,因此该命令行不起作用。

& 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\arangoimp.exe' --file 
'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json'
--type json --collection users --progress true --overwrite true
--server.username root --server.password password

在我的情况下,我只是在添加换行符后运行同一命令行的不同版本以查看它们是否有效。我知道我可以在使用 IF 语句时开始一个新行。在管道对象 | 时,我也可以使用新行。我的假设是某处有一个 Powershell 脚本规则列表。当我最初开始使用 Powershell 时,我以为我曾在某个地方看到过它们,但不知道它现在在哪里。

最佳答案

您可以使用 ` 作为续行符[1]在一行的最末端(后面甚至不允许有空格),以便将其分布在多行中。

这是一个简化的例子:

& cmd.exe /c echo `
hi `
there

这相当于 & cmd.exe/c echo hi there 并产生 hi there

注意:

  • 由于 ` 字符在视觉上很微妙,而且由于意外在其后面放置字符很容易破坏语法,因此请考虑使用 array 作为另一种选择 - 见下文。

  • 单个命令必须在一行中,因此如果您想将它们分布在多行中,则需要 `,如上所示。

  • 但是,管道 中,您可以使用 | 结束该行并继续下一行,而无需 `;例如:

      Get-Date | # Because the line ends with |, parsing continues on the next line.
    Select-Object Day
    • PowerShell [Core] v7+ 中,您也可以将 | 放在(非常)next 行的开头:

          Get-Date  # PS v7+ only
      | Select-Object Day
  • 另外,如果个别参数expression mode 中创建一个新的解析上下文。 - 例如固有的多行 (...) 表达式或传递给 cmdlet 的脚本 block ({...}) - 你'也可以自由地将表达式传播到多行;例如:

      1, 2, 3 | ForEach-Object { # { starts a multiline-aware context
    $_ + 1
    }
  • hybrid case 是一个 array-literal 参数,它允许您在内部元素的 , 分隔符之后中断命令:

      Get-Date | Select-Object -Property Day,
    Year
  • 以表达式模式开始的语句总是允许跨越多行(尽管嵌入命令模式语句受到通常的限制):

      $foo =         # an assignment is parsed in expression mode
    'bar'

或者,考虑使用 array[2] 来传递参数,这允许您使用多行表达式模式语法将参数定义为单独的数组元素之前:

# Construct the array of arguments (using multiline expression syntax)...
$arguments = '/c',
'echo',
'hi there'

# ... and pass it to cmd.exe
& cmd.exe $arguments

注意:数组元素 hi there 被 PowerShell 作为 "hi there" 传递:它采用自动双引号来确保参数被识别为 单个目标程序的参数。

顺便说一句:为了调用 PowerShell 命令(相对于 外部程序,就像手头的情况一样),考虑构建 hashtable 中的参数 用于 splatting ,其中每个条目键指定一个目标参数name,对应的值是参数value(参数);例如:

# Define the hashtable representing the named arguments.
$argsHash = @{
Filter = 'T*'
File = $true
}
# Note the use of "@" instead of "$"
# Equivalent of:
# Get-ChildItem -Filter T* -File
Get-ChildItem @argsHash

[1] ` 是 PowerShell 的通用转义字符。放置在一行的最后,它的功能略有不同:它不是 escaping 后面的换行符(这意味着 保留 它作为文字),它有效地告诉PowerShell 删除它并将下一行视为当前行的延续。

[2] 此答案的早期形式推荐基于数组的 splatting , 在 PetSerAl 之前指出要调用外部程序,按原样使用数组就足够了。
虽然也可以使用 splatting,但如果数组元素之一是 --%,即停止解析符号(简而言之:只有在 splatting 时,--% 有其特殊含义)。
在调用 PowerShell 命令时,飞溅是一种有用的技术,但主要是在其 hash-table 形式中(参见上一个链接)。

关于powershell - 何时可以在我的 Powershell 命令中使用换行符(换行符)来提高可读性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49244289/

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