windows 全局变量
Any system administrator who spends a good bit of time in the command prompt or batch scripts is probably well aware of built in environment variables Windows offers (i.e. Path, WinDir, ProgramFiles, UserProfile, etc.). If you find yourself using a particular value over and over, wouldn’t it be great if you had your own variable which you can use the same way as the built in values?
任何在命令提示符或批处理脚本中花费大量时间的系统管理员都可能清楚地了解Windows提供的内置环境变量(即Path,WinDir,ProgramFiles,UserProfile等)。 如果您发现自己一遍又一遍地使用特定值,那么如果您拥有自己的变量并且可以使用与内置值相同的方式,那岂不是很好吗?
With a few clicks, you can create and maintain you own environment variables which are both global on the system and survive reboots.
只需单击几下,您就可以创建和维护自己的环境变量,这些变量既在系统上是全局的,又可以在重新引导后生存下来。
创建自定义系统环境变量 (Creating a Custom System Environment Variable)
Creating a new global system variable is quite simple and is one of those features hiding in plain sight. Please note the screenshots are for Windows Server 2008, however the process for most versions of Windows is almost identical with only a few of the screens different. In the Control Panel, open the System option (alternately, you can right-click on My Computer and select Properties). Select the “Advanced system settings” link.
创建新的全局系统变量非常简单,并且是隐藏在其中的那些功能之一。 请注意,屏幕截图是针对Windows Server 2008的,但是大多数Windows版本的过程几乎相同,只有少数几个屏幕不同。 在“控制面板”中,打开“系统”选项(或者,您可以右键单击“我的电脑”,然后选择“属性”)。 选择“高级系统设置”链接。
In the System Properties dialog, click “Environment Variables”.
在“系统属性”对话框中,单击“环境变量”。
In the Environment Variables dialog, click the New button underneath the “System variables” section.
在“环境变量”对话框中,单击“系统变量”部分下方的“新建”按钮。
Enter the name of your new variable as well the value and click OK.
输入新变量的名称以及值,然后单击“确定”。
You should now see your new variable listed under the “System variables” section. Click OK to apply the changes.
现在,您应该在“系统变量”部分下看到新变量。 单击确定以应用更改。
You can now access your new system environment variable like you would any other. You can use it from the command line or batch scripts without having to define it.
现在,您可以像访问其他系统变量一样访问新的系统环境变量。 您可以从命令行或批处理脚本中使用它,而无需对其进行定义。
使用自定义环境变量 (Using the Custom Environment Variable)
As stated above, your custom environment variable is no different than any other system variable as you can reference it from the command line and inside of scripts. For a quick example, consider this batch script:
如上所述,您的自定义环境变量与任何其他系统变量没有什么不同,因为您可以从命令行和脚本内部对其进行引用。 作为一个简单的示例,请考虑以下批处理脚本:
@ECHO OFF TITLE Global Environment Variable Test ECHO. ECHO System NotifyEmail value ECHO NotifyEmail = %NotifyEmail% ECHO. SETLOCAL ECHO Overriding global variable in this script… SET NotifyEmail=jfaulkner@otheremail.com ECHO NotifyEmail = %NotifyEmail% ECHO. ECHO Exiting override script… ENDLOCAL ECHO. ECHO System NotifyEmail value ECHO NotifyEmail = %NotifyEmail% ECHO. ECHO. ECHO. PAUSE
@ECHO OFF TITLE全局环境变量测试ECHO。 ECHO系统的NotifyEmail值ECHO NotifyEmail =%NotifyEmail%ECHO。 SETLOCAL ECHO此脚本中的重写全局变量…SET NotifyEmail=jfaulkner@otheremail.com ECHO NotifyEmail =%NotifyEmail%ECHO。 ECHO正在退出替代脚本…ENDLOCAL ECHO。 ECHO系统的NotifyEmail值ECHO NotifyEmail =%NotifyEmail%ECHO。 回声。 回声。 暂停
When executed, the output is exactly what you would expect:
执行后,输出正是您所期望的:
使用思路 (Usage Ideas)
The real power of custom environment variables enters when you use them in your scripts. In our example, we set a variable called “NotifyEmail” which we could reference in any number of scripts without having to hard code the value. So in the event we need to change the email address, we simply update the system variable and the impacted scripts will use this new value without us having to update each script individually.
当在脚本中使用自定义环境变量时,它们的真正作用就体现出来了。 在我们的示例中,我们设置了一个名为“ NotifyEmail”的变量,我们可以在任意数量的脚本中引用该变量,而无需对值进行硬编码。 因此,如果需要更改电子邮件地址,我们只需更新系统变量,受影响的脚本将使用此新值,而无需我们分别更新每个脚本。
This is not only a time saver, but also protects against the situation where you forget to update a particular script and a “dead” value is being used. Additionally, in the event you need to override a system variable in a particular script, you can see in our example above this is fully supported.
这不仅节省了时间,而且还可以避免因忘记更新特定脚本而使用“无效”值的情况。 此外,如果您需要在特定脚本中覆盖系统变量,则可以在上面的示例中看到,这是完全受支持的。
Here are some ideas where you could apply system variables in place of local scope variables:
这里有一些想法,您可以在其中应用系统变量代替局部范围变量:
- Email addresses (like in our example) 电子邮件地址(例如我们的示例)
- Backup folder locations 备份文件夹位置
- URL and FTP sitesURL和FTP站点
- Metric and threshold values指标和阈值
Another great feature about using system variables is you have a single place where you can edit or view your variable values. Simply put, you could potentially apply updates to multiple scripts by editing the environment variables in a single location.
使用系统变量的另一个重要功能是您可以在一个地方编辑或查看变量值。 简而言之,您可以通过在单个位置中编辑环境变量来将更新应用于多个脚本。
翻译自: https://www.howtogeek.com/51807/how-to-create-and-use-global-system-environment-variables/
windows 全局变量