windows 全局变量_如何在Windows中使用全局系统环境变量

windows 全局变量

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版本的过程几乎相同,只有少数几个屏幕不同。 在“控制面板”中,打开“系统”选项(或者,您可以右键单击“我的电脑”,然后选择“属性”)。 选择“高级系统设置”链接。

image

In the System Properties dialog, click “Environment Variables”.

在“系统属性”对话框中,单击“环境变量”。

image

In the Environment Variables dialog, click the New button underneath the “System variables” section.

在“环境变量”对话框中,单击“系统变量”部分下方的“新建”按钮。

image

Enter the name of your new variable as well the value and click OK.

输入新变量的名称以及值,然后单击“确定”。

image

You should now see your new variable listed under the “System variables” section. Click OK to apply the changes.

现在,您应该在“系统变量”部分下看到新变量。 单击确定以应用更改。

image

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.

现在,您可以像访问其他系统变量一样访问新的系统环境变量。 您可以从命令行或批处理脚本中使用它,而无需对其进行定义。

image

使用自定义环境变量 (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:

执行后,输出正是您所期望的:

image

使用思路 (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 sites

    URL和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 全局变量

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279048.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

twitter api使用_使用P2创建自己的Twitter风格的组博客

twitter api使用Would you like a great way to post stuff quickly online and communicate with your readers? Here’s how you can use the P2 theme to transform WordPress into a great collaboration and communications platform. 您是否想以一种很好的方式在网上快…

10_30_unittest

1、断言 1)、self.assertEqual(2,res)#期望值qian、结果值hou2)TextTestRunner 源码 必要的参数3)测试结果 上下文管理器 with open("test.txt",w,encodingutf-8) as file:runner unittest.TextTestRunner(streamfile,verbosity2)…

laravel graphql php,结合 Laravel 初步学习 GraphQL

本文字数:7134,大概需要14.27分钟。按照官网所述的:A query language for your API一种用于 API 的查询语言GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a…

wi-fi共享大师免广告_如何保护Wi-Fi网络免受入侵

wi-fi共享大师免广告Insecure Wi-Fi is the easiest way for people to access your home network, leech your internet, and cause you serious headaches with more malicious behavior. Read on as we show you how to secure your home Wi-Fi network. 不安全的Wi-Fi是人们…

【材质】色彩基础

RBG颜色空间 目前,绝大部分显示器采用的是RGB颜色标准,因此几乎所有软件也采用此标准,UE4也不例外。 R、G、B这三个字母分别代表红色(red)、绿色(green)、蓝色(blue)三条…

使用mintty(_如何使用Mintty改善Cygwin控制台

使用mintty(Cygwin’s great for getting some Linux command-line goodness in Windows, but using the Windows Shell to access it kills some of that magic. Using Mintty and a few other methods, you can make the experience much more luxurious. Cygwin非常适合在Wi…

18.phpmyadmin 4.8.1 远程文件包含漏洞(CVE-2018-12613)

phpmyadmin 4.8.1 远程文件包含漏洞(CVE-2018-12613) phpMyAdmin是一套开源的、基于Web的MySQL数据库管理工具。其index.php中存在一处文件包含逻辑, 通过二次编码即可绕过检查,造成远程文件包含漏洞。 受影响版本: phpMyAdmin 4.…

关于“Python”的核心知识点整理大全38

14.1.1 创建 Button 类 由于Pygame没有内置创建按钮的方法,我们创建一个Button类,用于创建带标签的实心矩形。 你可以在游戏中使用这些代码来创建任何按钮。下面是Button类的第一部分,请将这个类保存为 文件button.py: button.py …

同步您的Google Chrome书签,主题等

Do you regularly use Google Chrome on multiple computers? Here’s how you can keep almost everything in your browser synced easily in Google Chrome. 您是否经常在多台计算机上使用Google Chrome? 您可以通过以下方法在Google Chrome浏览器中轻松同步浏…

找call写call_如何将Google Call Widget添加到任何网页

找call写callAdding a Google Call Widget to your website or blog allows visitors to contact you using your Google Voice number. The widget provides an easy and cost-effective way to provide live customer support without the customer knowing your real number…

XML与web开发-01- 在页面显示和 XML DOM 解析

前言: 关于 xml 特点和基础知识,可以菜鸟教程进行学习:http://www.runoob.com/xml/xml-tutorial.html 本系列笔记,主要介绍 xml 在 web 开发时需要了解的知识 XML 在页面显示数据 XML 指可扩展标记语言(eXtensible Mar…

酷安应用市场php源码,酷安应用市场 v11.0.3-999 去广告极限精简版

酷安,真实有趣的数码社区。酷安app,国内安卓应用市场客户端,应用资源丰富,应用开发者水准高,应用无首发Logo,原汁原味上架,得到了安卓用户群广泛认可。有人说现在的酷安市场(酷安网)没有以前那么…

chromebook刷机_如何将网站添加到您的Chromebook架子上

chromebook刷机Bookmarks are great to keep your favorite sites nearby, but they aren’t the fastest option out there. Instead, why not add shortcuts for your favorite websites right on the Chromebook shelf? 书签可以很好地将您喜欢的网站保留在附近&#xff0c…

Locktopus锁定iOS设备上的单个应用程序

If you want to share a cool game on your iOS device but not let everyone read your email, Locktopus offers a simple app-by-app lockdown solution. 如果您想在iOS设备上共享一个很棒的游戏,但又不想让所有人都阅读您的电子邮件,那么Locktopus提…

视频翻录_将DVD解密并复制到硬盘驱动器而无需翻录

视频翻录Have you ever wanted to make backup copies of your DVDs but didn’t want to mess with confusing DVD ripping software? Today, we’ll look at drop dead simple method to decrypt DVDs on the fly with DVD43 so you can easily copy them to your hard dri…

详解面向对象、构造函数、原型与原型链

详解面向对象、构造函数、原型与原型链 为了帮助大家能够更加直观的学习和了解面向对象,我会用尽量简单易懂的描述来展示面向对象的相关知识。并且也准备了一些实用的例子帮助大家更加快速的掌握面向对象的真谛。 jQuery的面向对象实现封装拖拽简易版运动框架封装这…

如何将Wii遥控器用作陀螺仪鼠标

If you have a spare Nintendo Wii remote with the Motion Plus add-on, you can use it to control your Windows PC from across the room. Here’s how to get it working in a couple of easy steps. 如果您有带Motion Plus附加组件的备用Nintendo Wii遥控器,则…

php 自带 web server 如何重写 rewrite .htaccess

为什么80%的码农都做不了架构师&#xff1f;>>> <?php // filename: route.php if (preg_match(/\.(?:png|jpg|jpeg|gif|css|js)$/, $_SERVER["REQUEST_URI"])) {return false; } else {include __DIR__ . /index.php; } 更好的写法&#xff1a; &l…

sci-hub谷歌插件_Google Home Hub具有隐藏屏幕设置菜单

sci-hub谷歌插件You can adjust the brightness or set an alarm on your Google Home Hub with a voice command. But if you’re trying to be quiet or there’s a lot of background noise, you can also do these things using a hidden Screen Settings menu. 您可以使用…

二叉树的前序、中序、后序遍历与创建

#include <iostream> #include <string> #include <stack> using namespace std; struct node; typedef node *pNode; struct node { char data; pNode left, right; }; string line; string::iterator it; // 前序扩展序列建立二叉树 void plan…