在Powershell中更新变量中的变量:

huangapple go评论118阅读模式
英文:

Updating variables within variables in Powershell

问题

我不太了解PowerShell,但我尝试学习。
你能帮助我理解和解决我想做的事情吗:

  1. $string1 = ""
  2. $complicatedString1 = "这是一个复杂的 $string1"
  3. $complicatedString2 = "$complicatedString1 也是"
  4. $string1 = "测试"
  5. $complicatedString1 -> 现在应该是: 这是一个复杂的 测试
  6. $complicatedString2 -> 现在应该是: 这是一个复杂的 测试 也是
  7. $string1 = "问题"
  8. $complicatedString1 -> 现在应该是: 这是一个复杂的 问题
  9. $complicatedString2 -> 现在应该是: 这是一个复杂的 问题 也是
  10. 等等

这个想法听起来很简单:我想定义作为模板的字符串,但带有可变内容。在这种情况下,我想操作$string1并更新$complicatedString1和$complicatedString2,以便$string1的任何更改都会反映在这些字符串中。

$string1会经常更改,目前我还没有方法将更改后的值传递到这些字符串中。基本上它们只是等待被更改的占位符。

英文:

I don't know much about Powershell but I try to learn.
Can you help me to understand and solve what I want to do:

  1. $string1=""
  2. $complicatedString1 = "This is a complicated $string1"
  3. $complicatedString2 = "$complicatedString1 too"
  4. $string1 = "Test"
  5. $complicatedString1 -> Should state now: This is a complicated Test
  6. $complicatedString2 -> Should state now: This is a complicated Test too.
  7. $string1 = "question"
  8. $complicatedString1 -> Should state now: This is a complicated question
  9. $complicatedString2 -> Should state now: This is a complicated question too.
  10. and so on.

The idea sounds simple: I want to define strings that serve as templates, but with variable content.
In this case I want to manipulate $string1 and update $complicatedstring 1 and 2 so that any change of $string1 gets reflected in those strings.

$string1 would change frequently and at the moment I have no approach how get the changed values into those strings. Basically the're just placeholders to wait for being changed.

答案1

得分: 1

我不太确定,但我认为你的问题是关于如何使用可以更改的模板字符串。

我认为最优雅的方法是利用PowerShell的-f格式运算符,如下所示:

  1. # 模板字符串
  2. $complicatedString1 = "这是一个复杂的 {0}"
  3. $complicatedString2 = "{0} 也是"
  4. # 使用模板字符串返回新字符串
  5. $string1 = $complicatedString1 -f "测试" # --> "这是一个复杂的测试"
  6. $string2 = $complicatedString2 -f $string1 # --> "这是一个复杂的测试 也是"
  7. $string1 = $complicatedString1 -f "问题" # --> "这是一个复杂的问题"
  8. $string2 = $complicatedString2 -f $string1 # --> "这是一个复杂的问题 也是"

你也可以使用字符串对象自己的Format方法,如果你喜欢的话,它会给你完全相同的结果。

英文:

I'm not quite sure, but I think your question is about using template strings you can alter using new strings-to-insert.

The most elegant way to do that I think is by making use of the PowerShell -f format operator like this:

  1. # the template strings
  2. $complicatedString1 = "This is a complicated {0}"
  3. $complicatedString2 = "{0} too"
  4. # use the template strings to return new strings
  5. $string1 = $complicatedString1 -f "Test" # --> "This is a complicated Test"
  6. $string2 = $complicatedString2 -f $string1 # --> "This is a complicated Test too"
  7. $string1 = $complicatedString1 -f "question" # --> "This is a complicated question"
  8. $string2 = $complicatedString2 -f $string1 # --> "This is a complicated question too"

You can also use the Format method of the string object itself if you like:

  1. $string1 = [string]::Format($complicatedString1, "Test")
  2. $string2 = [string]::Format($complicatedString2, $string1)

which will give you the exact same results

答案2

得分: 0

为此,您需要将 complicatedString1complicatedString2 放在单引号中(否则它们将直接展开),然后可以使用 $ExecutionContext.InvokeCommand.ExpandString 方法:

  1. $complicatedString1 = '这是一个复杂的 $string1'
  2. $complicatedString2 = '$complicatedString1 也是'
  3. $string1 = '测试'
  4. $ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
  5. 这是一个复杂的 测试
  6. $ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
  7. 这是一个复杂的 测试 也是
  8. $string1 = '问题'
  9. $ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
  10. 这是一个复杂的 问题
  11. $ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
  12. 这是一个复杂的 问题 也是
  13. *请注意您需要两次在 `$complicatedString2` 上使用 `ExpandString` 方法才能获得 `$string1` 展开的最终结果*
  14. 为了更好地了解情况您可以考虑创建一个带有 `-Depth` 参数的递归函数
  15. ```powershell
  16. Function Expand([String]$String, [Int]$Depth = 1) {
  17. $Expand = $ExecutionContext.InvokeCommand.ExpandString($String)
  18. If ($Depth-- -ge 0) {Expand $Expand $Depth} Else {$Expand}
  19. }
  20. $string1 = '测试'
  21. Expand $complicatedString1
  22. 这是一个复杂的 测试
  23. Expand $complicatedString2 -Depth 2
  24. 这是一个复杂的 测试 也是
英文:

For this you need to put you complicatedString1 and complicatedString2 in single quotes (otherwise they will be directly expanded), than you might use the $ExecutionContext.InvokeCommand.ExpandString method:

  1. $complicatedString1 = 'This is a complicated $string1'
  2. $complicatedString2 = '$complicatedString1 too'
  3. $string1 = 'Test'
  4. $ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
  5. This is a complicated Test
  6. $ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
  7. This is a complicated Test too
  8. $string1 = "question"
  9. $ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
  10. This is a complicated question
  11. $ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
  12. This is a complicated question too

Note that you will need to use the ExpandString method twice on the $complicatedString2 to get to the bottom of the $string1 expansion.

To get a better overview, you might consider to create a recursive function with a -Depth parameter:

  1. Function Expand([String]$String, [Int]$Depth = 1) {
  2. $Expand = $ExecutionContext.InvokeCommand.ExpandString($String)
  3. If ($Depth-- -ge 0) {Expand $Expand $Depth} Else {$Expand}
  4. }
  5. $string1 = 'Test'
  6. Expand $complicatedString1
  7. This is a complicated Test
  8. Expand $complicatedString2 -Depth 2
  9. This is a complicated Test too

答案3

得分: 0

你也可以通过循环来实现它。

  1. do {
  2. $string1 = Read-Host "请输入一个单词"
  3. $complicatedString1 = "这是一个复杂的 $string1"; $complicatedString1
  4. $complicatedString2 = "$complicatedString1 too`n"; $complicatedString2
  5. } while ($string1 -ne "退出程序")

输出

  1. 请输入一个单词:测试
  2. 这是一个复杂的 测试
  3. 这是一个复杂的 测试 too
  4. 请输入一个单词:问题
  5. 这是一个复杂的 问题
  6. 这是一个复杂的 问题 too
  7. 请输入一个单词:退出程序
  8. 这是一个复杂的 退出程序
  9. 这是一个复杂的 退出程序 too
  10. #程序退出#
英文:

You can also do it via a loop

  1. do {
  2. $string1 = Read-Host "Please enter a word"
  3. $complicatedString1 = "This is a complicated $string1"; $complicatedString1
  4. $complicatedString2 = "$complicatedString1 too`n"; $complicatedString2
  5. } while ($string1 -ne "exit program")

Output

  1. Please enter a word: Test
  2. This is a complicated Test
  3. This is a complicated Test too
  4. Please enter a word: Question
  5. This is a complicated Question
  6. This is a complicated Question too
  7. Please enter a word: Exit Program
  8. This is a complicated Exit Program
  9. This is a complicated Exit Program too
  10. #Program Exits#

huangapple
  • 本文由 发表于 2020年1月3日 16:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定