脚本环境影响父shell [英] Script environment affects parent shell

查看:62
本文介绍了脚本环境影响父shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PowerShell 脚本中修改某些方面似乎会修改正在运行的 shell.如果我提供脚本而不是运行它,这将是意料之中的.

Modifying certain aspects in PowerShell scripts seem to modify the running shell. This would be expected if I source the script instead of running it.

script.ps1

[cultureinfo]::currentculture = [cultureinfo]::InvariantCulture
Set-PSDebug -Trace 1

> [cultureinfo]::currentculture

LCID             Name             DisplayName
----             ----             -----------
1031             de-DE            Deutsch (Deutschland)

> .\script.ps1
> [cultureinfo]::currentculture
DEBUG:    1+  >>>> [cultureinfo]::currentculture

LCID             Name             DisplayName
----             ----             -----------
127                               Unveränderliche Sprache (Unveränderliches Land bzw. unveränderliche Region)

很明显,调试跟踪是活跃的,并且文化变化持续存在......

So obviously debug tracing is active and cultural change persisted...

推荐答案

来自 [cultureinfo]::currentculture] 的文档(强调):

From the docs for [cultureinfo]::currentculture] (emphasis added):

获取或设置CultureInfo 对象,该对象表示当前线程使用的文化.

Gets or sets the CultureInfo object that represents the culture used by the current thread.

也就是说,设计上的更改对整个线程生效,而不仅仅是您的脚本,并且它会在线程的整个生命周期内(在您的脚本退出后)或直到您再次更改它为止.

That is, a change by design takes effect for the entire thread, not just your script, and it persists throughout the thread's lifetime (after your script exits) or until you change it again.

因此,如果您希望将您的文化更改范围,您必须手动:

Therefore, if you want your culture changes to be scoped, you must manually:

  • 事先保存以前的文化
  • 之后恢复这种文化.

警告:

  • Windows PowerShell 中,区域性会自动重置为启动值,但仅在命令提示符,在每次命令或脚本的调用 - 有关背景信息和解决方法,请参阅此答案;相比之下,如果一个脚本调用另一个脚本并且被调用者更改了当前区域性,则该更改对调用脚本仍然有效.

  • In Windows PowerShell, the culture is automatically reset to the startup value, but only at the command prompt, after every invocation of a command or script - see this answer for background information and a workaround; by contrast, if one script calls another and the callee changes the current culture, that change stays in effect for the calling script.

相比之下,PowerShell Core 行为一致,从不自动重置当前区域性.

By contrast, PowerShell Core behaves consistently, and never resets the current culture automatically.

请注意,行为类似于使用 Set-Location (cd) 更改当前位置(目录),这也会影响整个线程,因为它确实如此在 cmd.exe 批处理文件中(除非您使用 setlocal) - 但不是在 Bash 脚本中,例如.

Note that the behavior is similar to using Set-Location (cd) to change the current location (directory), which also affects the entire thread, as it does in a cmd.exe batch file (except if you use setlocal) - but not in a Bash script, for instance.

在 PowerShell 中,脚本文件 (*.ps1) 运行进程内,批处理文件 (*.cmd, *.bat) 在 cmd.exe 中,而类似 POSIX 的 shell(例如 Bash)在 子进程中运行脚本,它隐式地且总是将环境更改范围限定为此类脚本.

In PowerShell, script files (*.ps1) run in-process, as do batch files (*.cmd, *.bat) in cmd.exe, whereas POSIX-like shells such as Bash run scripts in a child process, which implicitly and invariably scopes environment changes to such scripts.

这篇关于脚本环境影响父shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆