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
Batch file 将多个变量传递到二进制文件_Batch File - Fatal编程技术网

Batch file 将多个变量传递到二进制文件

Batch file 将多个变量传递到二进制文件,batch-file,Batch File,在unix命令行中可以执行此操作 tempvar=ABC mybinary 显然,您可以在Windows批处理中执行相同的操作,但如果需要通过多个分配,unix中的export就可以了。在Windows批处理脚本中,如何将多个变量传递给mybynary。有关Windows命令和Windows命令处理器语法的信息 可以在输出中执行help,以获得不完整的Windows命令列表,并提供简要说明。任何Windows命令都可以使用/?作为参数执行,以获取命令的使用帮助输出。执行cmd/?会在Linux

在unix命令行中可以执行此操作

tempvar=ABC mybinary
显然,您可以在Windows批处理中执行相同的操作,但如果需要通过多个分配,unix中的
export
就可以了。在Windows批处理脚本中,如何将多个变量传递给
mybynary
。有关Windows命令和Windows命令处理器语法的信息 可以在输出中执行
help
,以获得不完整的Windows命令列表,并提供简要说明。任何Windows命令都可以使用
/?
作为参数执行,以获取命令的使用帮助输出。执行
cmd/?
会在Linux终端窗口中输出Windows命令处理器
cmd.exe
的使用帮助,这是Windows中相当于
man sh
(或
man bash
、man
dash
man ksh
等)的代码

对于大多数Windows命令(但不是所有命令),也可以使用命令名作为参数运行
help
help set
set/?
输出命令的用法帮助以定义环境变量

此外,还有:

  • 有关
  • 通过SS64的
  • 通过SS64的
还为Windows、Linux和Mac上的其他脚本语言提供全面的文档

有两种类型的Windows命令:

  • cmd.exe
    的内部命令,如
    echo
    copy
    set
  • 所谓的外部命令,如
    chcp
    find
    robocopy
  • 外部Windows命令是目录
    %SystemRoot%\System32
    中的可执行文件,文件扩展名为
    .com
    类似于
    chcp.com
    (罕见)或文件扩展名为
    .exe
    类似于
    find.exe
    robocopy.exe
    where.exe
    (大多数外部Windows命令)

    64位Windows上有两个系统目录:

    • %SystemRoot%\System32
      包含64位可执行文件和动态链接库
    • %SystemRoot%\SysWOW64
      包含32位可执行文件和动态链接库
    Windows命令通常以32位和64位可执行文件的形式存在于64位Windows上的两个系统目录中。但有些可执行文件仅作为64位可执行文件存在

    要知道32位应用程序是否在64位Windows上启动Windows命令处理器
    cmd.exe
    来处理批处理文件,这一点很重要,因为在这种情况下,32位
    %SystemRoot%\SysWOW64\cmd.exe
    以及通过文件夹中的环境变量
    PATHEXT
    PATH
    找到的所有命令都会启动默认情况下,
    %SystemRoot%\System32
    系统环境变量
    路径
    中的第一个文件夹路径,在64位窗口的32位执行环境中,从目录
    %SystemRoot%\SysWOW64
    中实际搜索和执行

    在命令提示窗口中,用户输入的命令通常只有文件名,因此Windows命令处理器必须搜索合适的文件来执行。有关此过程的详细信息,请阅读:

    在批处理文件中,也只能使用外部Windows命令的文件名,但出于以下原因,建议使用完全限定的文件名指定它们:

  • 批处理文件由外部命令指定,如
    %SystemRoot%\System32\find.exe
    而不是
    find
    ,具有更高的故障安全性,因为批处理文件不依赖于批处理文件外部定义的环境变量
    pathText
    PATH
    ,这些变量通常由程序和程序修改用户,尤其是环境变量
    PATH
  • 批处理文件的执行速度更快,因为Windows命令处理器不必在文件系统中搜索可执行文件
  • 外部Windows命令
    ,其中
    可用于确定Windows命令是内部命令还是外部命令。在命令提示窗口中执行
    where set
    将导致输出错误消息(希望如此),而
    where find
    将导致输出文件限定文件名
    find
    (希望首先在文件扩展名为
    %SystemRoot%\System32
    .exe
    中找到)

    2.为可执行文件定义一个或多个环境变量 在Windows上定义环境变量的命令是SET。因此,必须使用此命令定义可执行文件在执行时对其求值的环境变量。像
    tempvar
    这样的环境变量可以定义如下:

  • set tempvar=ABC
  • 设置“tempvar=ABC”
  • 强烈建议使用第二种语法,但需要启用命令扩展(默认情况下是这样),但这并不意味着总是启用命令扩展。请阅读我的回答:有详细解释为什么
    set“variable=value”
    是推荐的语法

    可执行文件评估的环境变量可以在执行可执行文件之前在单独的行上定义,也可以在同一命令行上定义

    环境变量的定义和可执行文件在单独的命令行上的执行:

    set "tempvar=ABC"
    mybinary
    
    在一个命令行上定义环境变量和执行可执行文件:

    set "tempvar=ABC" & mybinary
    
    解释单个命令行变量中使用的运算符
    &

    批处理文件编写器应避免替换其中一个预定义的环境变量,除非在批处理脚本中有意重新定义预定义的环境变量

    3.仅为execut定义一个或多个环境变量
    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    rem The two command lines above define completely the execution environment for the
    rem batch file and so the batch file does not depend anymore on settings outside of
    rem the batch file.
    
    rem Define MyVariable1 and undefine MyVariable2
    set "MyVariable1=Demo"
    set "MyVariable2="
    rem Verify that with an output of all environment variables of
    rem which name starts with the string MyVariable with their values.
    echo/
    echo My variables defined in batch script at top:
    echo/
    set MyVariable
    
    setlocal
    set "MyVariable1=Other value for execuable"
    set "MyVariable2=Defined also for executable"
    echo/
    echo My variables defined for mybinary:
    echo/
    set MyVariable
    if exist mybinary mybinary
    endlocal
    
    echo/
    echo My variables defined after execution of mybinary:
    echo/
    set MyVariable
    
    endlocal
    
    echo/
    echo My variables as defined outside of the batch file:
    echo/
    set MyVariable
    echo/
    pause