Batch file 将多个变量传递到二进制文件
在unix命令行中可以执行此操作Batch file 将多个变量传递到二进制文件,batch-file,Batch File,在unix命令行中可以执行此操作 tempvar=ABC mybinary 显然,您可以在Windows批处理中执行相同的操作,但如果需要通过多个分配,unix中的export就可以了。在Windows批处理脚本中,如何将多个变量传递给mybynary。有关Windows命令和Windows命令处理器语法的信息 可以在输出中执行help,以获得不完整的Windows命令列表,并提供简要说明。任何Windows命令都可以使用/?作为参数执行,以获取命令的使用帮助输出。执行cmd/?会在Linux
tempvar=ABC mybinary
显然,您可以在Windows批处理中执行相同的操作,但如果需要通过多个分配,unix中的export
就可以了。在Windows批处理脚本中,如何将多个变量传递给mybynary
。有关Windows命令和Windows命令处理器语法的信息
可以在输出中执行help
,以获得不完整的Windows命令列表,并提供简要说明。任何Windows命令都可以使用/?
作为参数执行,以获取命令的使用帮助输出。执行cmd/?
会在Linux终端窗口中输出Windows命令处理器cmd.exe
的使用帮助,这是Windows中相当于man sh
(或man bash
、mandash
、man ksh
等)的代码
对于大多数Windows命令(但不是所有命令),也可以使用命令名作为参数运行help
help set
和set/?
输出命令的用法帮助以定义环境变量
此外,还有:
- 有关
- 通过SS64的
- 通过SS64的
cmd.exe
的内部命令,如echo
,copy
,set
chcp
,find
,robocopy
%SystemRoot%\System32
中的可执行文件,文件扩展名为.com
类似于chcp.com
(罕见)或文件扩展名为.exe
类似于find.exe
,robocopy.exe
和where.exe
(大多数外部Windows命令)
64位Windows上有两个系统目录:
包含64位可执行文件和动态链接库%SystemRoot%\System32
包含32位可执行文件和动态链接库%SystemRoot%\SysWOW64
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命令是内部命令还是外部命令。在命令提示窗口中执行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