Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs应用程序没有';无法识别之前设置的批处理环境变量_Node.js_Batch File_Variables - Fatal编程技术网

Node.js nodejs应用程序没有';无法识别之前设置的批处理环境变量

Node.js nodejs应用程序没有';无法识别之前设置的批处理环境变量,node.js,batch-file,variables,Node.js,Batch File,Variables,因此,我有一个批处理文件,它提示用户输入,然后将其存储为一个变量,如下所示: set /p name = "Enter name" node app.js %name% 然后,我需要调用node js应用程序,并将该变量作为参数传递,如下所示: set /p name = "Enter name" node app.js %name% 但是当我在节点应用程序中console.log(name)时,它是未定义的 我该怎么做 如果我手动传递参数,它可以正常工作。i、 e.节点app.js t

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

set /p name = "Enter name"
node app.js %name%
然后,我需要调用node js应用程序,并将该变量作为参数传递,如下所示:

set /p name = "Enter name"
node app.js %name%
但是当我在节点应用程序中
console.log(name)
时,它是
未定义的

我该怎么做

如果我手动传递参数,它可以正常工作。i、 e.
节点app.js testName

set /p name = "Enter name"
哪个输出

 "Enter name"
当批处理用户在此异常提示下输入字符串时,定义一个名为
name
的环境变量变量名以空格字符结尾

上的答案详细说明了如何将字符串分配给环境变量right

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

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

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

    演示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
    时,也可以使用
    设置/P variable=“prompt”
    ,因为Windows命令处理器具有特殊的额外提示文本处理功能

    但请注意,
    set variable=“value”
    将带有双引号的
    “value”
    以及批处理文件中可能存在的行尾空格/制表符分配给环境变量

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

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


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

    请去掉等号周围的空格。这是关于批处理的唯一问题。正如拉面所说:
    set/p name=“Enter name”
    100%正确的批处理命令行的副本将是:
    set/p”name=Enter name:“
    @Mofi,对于
    set/p
    ,通常的最佳语法是
    set/p name=“Enter name:
    ”,因为这允许在提示文本中包含周围的引号(如
    set/P name=“%text%”
    ;当
    %text%
    等于
    ”某些消息时:“
    ;保留引号)。。。