nodejs应用程序不能识别之前设置的批处理环境variables

所以我有一个batch file,提示用户input,然后存储一个variables,如下所示:

set /p name = "Enter name" 

然后我需要调用一个节点js应用程序并将该variables作为parameter passing,如下所示:

 node app.js %name% 

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

我将如何使其工作?

它工作正常,如果我手动传递参数。 即node app.js testName

该线

 set /p name = "Enter name" 

哪个输出

  "Enter name" 

当批处理用户在这个exception提示中input一个string时,定义一个名称为name的环境variables。 variables名以空格字符结尾!

在命令行使用'set var = text'后 , 为什么没有使用'echo%var%'输出string? 详细解释如何将string分配给环境variables权限。

但是,在第一个等号被解释为环境variables名称和提示文本之间的分隔符之后,选项/P的使用会导致对string进行额外的string处理。

如果第一个等号后面的提示文字的第一个字符是双引号,那么

  1. Windows命令处理器删除这个双引号和
  2. 从提示文本的末尾到末尾search另一个双引号,并跳过尾随空格/制表符,并将提示文本截断为提示文本最后一个双引号的位置。

如果第一个等号后面的第一个字符不是双引号字符,则会按batch file中的定义打印提示文本。

用于演示Windows命令处理器提示文本操作的示例批处理代码:

 @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 

注意:尾部空格和制表符在这里看不到,但是在运行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"强烈build议将一个string赋值给一个环境variables。 但是在使用/P ,也可以使用set /P variable="prompt"因为Windows命令处理器有特殊的附加提示文本处理。

但是请注意, set variable="value"会将带有双引号的"value" batch file中可能存在的尾随空格/制表符分配给环境variables。

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

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

要么

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

但是我觉得这样的提示很less被通缉。