nodejs app无法识别之前设置的批处理环境变量

时间:2016-10-02 22:20:33

标签: node.js batch-file variables

所以我有一个批处理文件提示用户输入,然后按如下方式存储变量:

set /p name = "Enter name"

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

node app.js %name%

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

我将如何使其发挥作用?

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

1 个答案:

答案 0 :(得分:1)

该行

set /p name = "Enter name"

输出

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

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

Why is no string output with 'echo %var%' after using 'set var = text' on command line?上的答案详细解释了如何将字符串分配给环境变量。

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

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

  1. Windows命令处理器删除此双引号和
  2. 从跳过尾部空格/制表符的另一个双引号的提示文本的结尾搜索,并将提示文本截断到提示文本的最后双引号的位置。
  3. 如果第一个等号后面的第一个字符不是双引号字符,则按批处理文件中的定义打印提示文本。

    用于演示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
    

    注意:此处无法显示尾随空格和标签,但在运行批处理文件时输出:

    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时,由于Windows命令处理器的特殊附加提示文本处理,因此也可以使用set /P variable="prompt"

    但请注意,set variable="value""value" 分配双引号并且可能存在于批处理文件中的尾随空格/制表符环境变量。

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

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

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

    但我认为很少需要这样的提示。