我需要在运行批处理文件时传递一个ID和密码,而不是将它们硬编码到文件中。
下面是命令行的样子:
test.cmd admin P@55w0rd > test-log.txt
我需要在运行批处理文件时传递一个ID和密码,而不是将它们硬编码到文件中。
下面是命令行的样子:
test.cmd admin P@55w0rd > test-log.txt
当前回答
成对的参数
如果你喜欢在键值对中传递参数,你可以使用这样的方法:
@echo off
setlocal enableDelayedExpansion
::::: asigning arguments as a key-value pairs:::::::::::::
set counter=0
for %%# in (%*) do (
set /a counter=counter+1
set /a even=counter%%2
if !even! == 0 (
echo setting !prev! to %%#
set "!prev!=%%~#"
)
set "prev=%%~#"
)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: showing the assignments
echo %one% %two% %three% %four% %five%
endlocal
举个例子:
c:>argumentsDemo.bat one 1 "two" 2 three 3 four 4 "five" 5
1 2 3 4 5
预定义的变量
您还可以提前设置一些环境变量。这可以通过在控制台或在我的电脑上设置来完成:
@echo off
if defined variable1 (
echo %variable1%
)
if defined variable2 (
echo %variable2%
)
像这样称呼它:
c:\>set variable1=1
c:\>set variable2=2
c:\>argumentsTest.bat
1
2
包含列出值的文件
您还可以指向预先设置所需值的文件。 如果这是脚本:
@echo off
setlocal
::::::::::
set "VALUES_FILE=E:\scripts\values.txt"
:::::::::::
for /f "usebackq eol=: tokens=* delims=" %%# in ("%VALUES_FILE%") do set "%%#"
echo %key1% %key2% %some_other_key%
endlocal
values文件是这样的:
:::: use EOL=: in the FOR loop to use it as a comment
key1=value1
key2=value2
:::: do not left spaces arround the =
:::: or at the begining of the line
some_other_key=something else
and_one_more=more
调用它的输出将是:
Value1 value2别的东西
当然,您可以结合所有方法。检查参数语法,shift
其他回答
如果你担心安全/密码盗窃(导致你设计这个解决方案,需要登录凭证在执行而不是静态的硬编码不需要数据库),然后您可以存储api或一半密码解密的代码或程序文件解密密钥,所以在运行时,用户将在控制台输入用户名/密码散列/解密之前传递给程序代码执行通过设置/ p,如果你查看在运行时用户输入凭据。
如果您正在运行一个脚本,使用不同的用户/密码运行程序,那么命令行参数将适合您。
如果您正在制作一个测试文件来查看不同登录的输出/效果,那么您可以将所有登录存储在一个加密文件中,作为参数传递给test。Cmd,除非你想坐在命令行并输入所有的登录,直到完成。
可以提供的参数数量限制为命令行上的字符总数。为了克服这一限制,上一段技巧是一种变通方法,不会有暴露用户密码的风险。
我写了一个简单的read_params脚本,可以作为函数(或外部.bat)调用,并将所有变量放入当前环境中。它不会修改原始形参,因为函数是用原始形参的副本调用的。
例如,给定以下命令:
myscript.bat some -random=43 extra -greeting="hello world" fluff
Myscript.bat将能够在调用函数后使用变量:
call :read_params %*
echo %random%
echo %greeting%
函数如下:
:read_params
if not %1/==/ (
if not "%__var%"=="" (
if not "%__var:~0,1%"=="-" (
endlocal
goto read_params
)
endlocal & set %__var:~1%=%~1
) else (
setlocal & set __var=%~1
)
shift
goto read_params
)
exit /B
限制
不能加载无值的参数,例如-force。你可以使用-force=true,但我想不出一种方法来允许空值,而不知道提前知道一个参数列表,不会有一个值。
更新日志
2/18/2016 不再需要延迟扩展 现在通过查找- before形参来处理其他命令行参数。
对于使用循环获取所有参数并纯批处理:
注意事项:用于不带:?*&|<>
@echo off && setlocal EnableDelayedExpansion
for %%Z in (%*)do set "_arg_=%%Z" && set/a "_cnt+=1+0" && (
call set "_arg_[!_cnt!]=!_arg_!" && for /l %%l in (!_cnt! 1 !_cnt!
)do echo/ The argument n:%%l is: !_arg_[%%l]!
)
goto :eof
你的代码已经准备好在需要的地方用参数号做一些事情,比如……
@echo off && setlocal EnableDelayedExpansion
for %%Z in (%*)do set "_arg_=%%Z" && set/a "_cnt+=1+0" && call set "_arg_[!_cnt!]=!_arg_!"
fake-command /u !_arg_[1]! /p !_arg_[2]! > test-log.txt
批处理文件
set argument1=%1
set argument2=%2
echo %argument1%
echo %argument2%
%1和%2分别返回第一个和第二个参数值。
在命令行中,传递参数
Directory> batchFileName admin P@55w0rd
输出将是
admin
P@55w0rd
是的,在使用if和for和the gang时,不要忘记使用%%1这样的变量。
如果您忘记使用double %,那么您将替换(可能为空)命令行参数,并且您将收到一些非常令人困惑的错误消息。