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

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

Updating variables within variables in Powershell

问题

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

$string1 = ""

$complicatedString1 = "这是一个复杂的 $string1"
$complicatedString2 = "$complicatedString1 也是"

$string1 = "测试"

$complicatedString1 -> 现在应该是: 这是一个复杂的 测试
$complicatedString2 -> 现在应该是: 这是一个复杂的 测试 也是

$string1 = "问题"

$complicatedString1 -> 现在应该是: 这是一个复杂的 问题
$complicatedString2 -> 现在应该是: 这是一个复杂的 问题 也是

等等

这个想法听起来很简单:我想定义作为模板的字符串,但带有可变内容。在这种情况下,我想操作$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:

$string1=""

$complicatedString1 = "This is a complicated $string1"
$complicatedString2 = "$complicatedString1 too"

$string1 = "Test"

$complicatedString1 -> Should state now: This is a complicated Test
$complicatedString2 -> Should state now: This is a complicated Test too.

$string1 = "question"

$complicatedString1 -> Should state now: This is a complicated question
$complicatedString2 -> Should state now: This is a complicated question too.

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格式运算符,如下所示:

# 模板字符串
$complicatedString1 = "这是一个复杂的 {0}"
$complicatedString2 = "{0} 也是"

# 使用模板字符串返回新字符串
$string1 = $complicatedString1 -f "测试"                # --> "这是一个复杂的测试"
$string2 = $complicatedString2 -f $string1              # --> "这是一个复杂的测试 也是"

$string1 = $complicatedString1 -f "问题"            # --> "这是一个复杂的问题"
$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:

# the template strings
$complicatedString1 = "This is a complicated {0}"
$complicatedString2 = "{0} too"

# use the template strings to return new strings
$string1 = $complicatedString1 -f "Test"                # --> "This is a complicated Test"
$string2 = $complicatedString2 -f $string1              # --> "This is a complicated Test too"

$string1 = $complicatedString1 -f "question"            # --> "This is a complicated question"
$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:

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

which will give you the exact same results

答案2

得分: 0

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

$complicatedString1 = '这是一个复杂的 $string1'
$complicatedString2 = '$complicatedString1 也是'

$string1 = '测试'

$ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
这是一个复杂的 测试
$ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
这是一个复杂的 测试 也是

$string1 = '问题'

$ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
这是一个复杂的 问题
$ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
这是一个复杂的 问题 也是

*请注意您需要两次在 `$complicatedString2` 上使用 `ExpandString` 方法才能获得 `$string1` 展开的最终结果*

为了更好地了解情况您可以考虑创建一个带有 `-Depth` 参数的递归函数

```powershell
Function Expand([String]$String, [Int]$Depth = 1) {
	$Expand = $ExecutionContext.InvokeCommand.ExpandString($String)
	If ($Depth-- -ge 0) {Expand $Expand $Depth} Else {$Expand}
}

$string1 = '测试'

Expand $complicatedString1
这是一个复杂的 测试
Expand $complicatedString2 -Depth 2
这是一个复杂的 测试 也是
英文:

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:

$complicatedString1 = 'This is a complicated $string1'
$complicatedString2 = '$complicatedString1 too'

$string1 = 'Test'

$ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
This is a complicated Test
$ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
This is a complicated Test too

$string1 = "question"

$ExecutionContext.InvokeCommand.ExpandString($complicatedString1)
This is a complicated question
$ExecutionContext.InvokeCommand.ExpandString($ExecutionContext.InvokeCommand.ExpandString($complicatedString2))
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:

Function Expand([String]$String, [Int]$Depth = 1) {
	$Expand = $ExecutionContext.InvokeCommand.ExpandString($String)
	If ($Depth-- -ge 0) {Expand $Expand $Depth} Else {$Expand}
}

$string1 = 'Test'

Expand $complicatedString1
This is a complicated Test
Expand $complicatedString2 -Depth 2
This is a complicated Test too

答案3

得分: 0

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

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

输出

请输入一个单词:测试
这是一个复杂的 测试
这是一个复杂的 测试 too

请输入一个单词:问题
这是一个复杂的 问题
这是一个复杂的 问题 too

请输入一个单词:退出程序
这是一个复杂的 退出程序
这是一个复杂的 退出程序 too
#程序退出#
英文:

You can also do it via a loop

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

Output

Please enter a word: Test
This is a complicated Test
This is a complicated Test too

Please enter a word: Question
This is a complicated Question
This is a complicated Question too

Please enter a word: Exit Program
This is a complicated Exit Program
This is a complicated Exit Program too
#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:

确定