在批处理文件中将空格分隔的数字连接在一起
我需要在批处理文件中随机生成多个共享特定范围的值,并将它们连接在一起,并用空格分隔。除了将空格分隔的数字连接在一起之外,一切正常。具体的问题是用空格连接数字,我该如何解决这个问题?
相关部分如下:
::Assume %minA%, %maxA% and %randB% are defined correctly.
set /a randA=(%random% %% %maxA%) + %minA%
set agent_counter=0
:start_agent_loop
if %agent_counter% equ %randA% goto end
set /a randApos=(%random% %% %randB%) + 1
::supposedly (if I recall), simply having a space between %agent_starts%
::and %randApos% should do the trick, this doesn't seem to be the case.
set agent_starts = %agent_starts% %randApos%
set /a agent_counter +=1
goto start_agent_loop
:end
echo -A %agent_starts%
理想情况下,这应该打印类似...
C:\Path\>genparams.bat
-A 2 4 5
目前我得到
-A
I need to randomly generate a number of values in a batch file that share a certain range and concatenate them together separated by spaces. Everything works except for concatenating the space-separated numbers together. The specific problematic issue is concatenating with the numbers with spaces, how do I go about this?
The relevent portion follows:
::Assume %minA%, %maxA% and %randB% are defined correctly.
set /a randA=(%random% %% %maxA%) + %minA%
set agent_counter=0
:start_agent_loop
if %agent_counter% equ %randA% goto end
set /a randApos=(%random% %% %randB%) + 1
::supposedly (if I recall), simply having a space between %agent_starts%
::and %randApos% should do the trick, this doesn't seem to be the case.
set agent_starts = %agent_starts% %randApos%
set /a agent_counter +=1
goto start_agent_loop
:end
echo -A %agent_starts%
Ideally, this should print something like...
C:\Path\>genparams.bat
-A 2 4 5
Currently I get
-A
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下网站 (SS64.com) 对于批处理脚本参考非常有价值。
SET、IF,你说吧。
SET 命令对于额外的空格不太宽容。另外, set /a 显然会检查所有字符串以查看它们是否是变量,因此我认为 maxA 和 minA 周围不需要 %。我假设 %random% 是一个特殊的批处理文件变量,它返回一个随机数,所以我没有从中删除它们。 (尽管你可能能够)
更改:
到:
并更改:
到:
这样做(并设置 minA=1,maxA=10,randB=5),我在第一次运行时得到了这个:“-A 3 4 3 ”。
The following website (SS64.com) is INVALUABLE for batch script reference.
There are excellent pages on SET, IF, you name it.
The SET command is not very forgiving with extra whitespace. Also, set /a will apparently check all strings to see if they are a variable, so I don't think the %'s are required around maxA and minA. I assumed %random% was a special batch file variable that returns a random number, so I did not remove them from that. (though you might be able to)
Change:
To:
And change:
To:
Doing that (and setting a minA=1,maxA=10,randB=5), I got this on my first run: "-A 3 4 3".