nodejs应用无法识别之前设置的批处理环境变量 [英] nodejs app doesn't recognise batch environment variable set before

查看:97
本文介绍了nodejs应用无法识别之前设置的批处理环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个批处理文件,提示用户输入,然后将其存储为以下变量:

So I have a batch file which prompts user input and then stores it a variable as follows:

set /p name = "Enter name"

然后我需要调用一个节点js应用程序,并将该变量作为参数传递,如下所示:

I then need to call a node js app and passing that variable as a parameter as follows:

node app.js %name%

但是当我在节点应用程序中console.log(name)时,它是undefined.

But when I console.log(name) in the node app, it is undefined.

我如何使它工作?

如果我手动传递参数,就可以了.即node app.js testName.

It works okay if I pass the arguments manually. i.e. node app.js testName.

推荐答案

set /p name = "Enter name"

输出

 "Enter name"

当批处理用户在此异常提示符下输入字符串时,

定义名称为name 的环境变量. 变量名称以空格字符结尾!

defines an environment variable with name name  when the batch user enters a string on this unusual prompt. The variable name ends with a space character!

为什么在命令行上使用"set var = text"后,为什么没有字符串与"echo%var%"一起输出?/a>详细说明了如何为环境变量分配一个字符串.

The answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explains in detail how to assign a string to an environment variable right.

但是使用选项/P会导致在将第一个等号解释为环境变量名称和提示文本之间的分隔符之后对字符串进行额外的字符串操作.

But the usage of option /P results in an additional string manipulation of the string after first equal sign being interpreted as separator between environment variable name and prompt text.

如果提示文本的第一个等号后的第一个字符是双引号,则

If the first character of the prompt text starting after first equal sign is a double quote, then

  1. Windows命令处理器删除了该双引号,并且
  2. 从提示文本的结尾到结尾搜索另一个双引号,并跳过尾部的空格/制表符,然后将提示文本截断到提示文本的最后一个双引号的位置.

如果第一个等号后的第一个字符不是双引号字符,则按照批处理文件中的定义打印提示文本.

The prompt text is printed as defined in the batch file if the first character after the first equal sign is not a double quote character.

示例批处理代码演示了Windows命令处理器的即时文本操作:

Example batch code to demonstrate prompt text manipulation by Windows command processor:

@echo off
setlocal
rem Most often used prompt syntax.
set /P Variable="Enter 1: "

rem Prompt text starts by mistake with a space character resulting
rem in printing the prompt text without removing the double quotes.
set /P Variable= "Enter 2: "

rem Prompt text has no closing double quote and ends with a space.
rem The double quote at beginning is removed, but not the space at end.
set /P Variable="Enter 3: 

rem Prompt text has closing double quote and there is a horizontal tab
rem at end of the command line. Double quotes and tab are removed on print.
set /P Variable="Enter 4: " 

rem Prompt text has double quotes, but does not start with a double
rem quote. Prompt text is printed exactly as defined in batch file.
set /P Variable=Enter "5: "

rem Prompt text has double quotes, but does not start with a double
rem quote and command line ends by mistake with a tab character. The
rem prompt text is printed exactly as defined in batch file with the tab.
set /P Variable=Enter "6: " 

rem Variable name plus prompt text is enclosed in double quotes and therefore
rem the trailing space and trailing tab at end of the command line are ignored.
rem Additionally the prompt text is also enclosed in double quotes resulting in
rem removing the first and last quote of the prompt text and the trailing space
rem and trailing tab of the prompt text. The rest of the prompt text is printed.
set /P "Variable=""Enter 7: " "     "   
rem               ^..printed.^
rem              ^..prompt string..^
rem    ^...entire parameter string..^
endlocal

注意:在此处看不到尾随空格和制表符,但在运行批处理文件时显示为输出:

Note: The trailing spaces and tabs can't be seen here, but in output on running the batch file:

Enter 1: 1
 "Enter 2: "2
Enter 3: 3
Enter 4: 4
Enter "5: "5
Enter "6: "     6
"Enter 7: " 7

因此,通常最好将set /P "variable=prompt"set "variable=value"一样使用,强烈建议将字符串分配给环境变量.但是在使用/P时,也可​​以使用set /P variable="prompt",因为Windows命令处理器具有特殊的附加提示文本处理.

So in general best is to use set /P "variable=prompt" like set "variable=value" strongly recommended on assigning a string to an environment variable. But on usage of /P it is also possible to use set /P variable="prompt" because of the special additional prompt text handling of Windows command processor.

但是请注意,set variable="value""value" 分配给双引号,并且可能将批处理文件中的行中现有的尾随空格/制表符分配给环境变量.

But please note that set variable="value" assigns "value" with the double quotes and with perhaps existing trailing spaces/tabs on line in batch file to the environment variable.

因此,我的建议是始终对命令 SET 使用"variable=value/prompt",而与选项/P无关,该选项用于快速使用或不用于简单分配. aschipfl 写道,只有一个例外:打印的提示文本应以双引号开头.在这种情况下,最好使用

Therefore my advice is to use always "variable=value/prompt" on command SET independent on option /P is used for prompt usage or is not used for a simple assignment. There is only one exception as aschipfl wrote: the printed prompt text should start with a double quote. In this case it is better to use

set /P Variable=""Prompt text in quotes: ""

set /P "Variable=""Prompt text in quotes: """

但是我认为很少有这样的提示.

But I think such a prompt is very rarely wanted.

这篇关于nodejs应用无法识别之前设置的批处理环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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