多循环做一个命令:呼应字符串到一个文件中,然后重定向到命令窗口 [英] Multiple do commands in a for loop: Echoing a string to a file and then redirecting to the command window

查看:153
本文介绍了多循环做一个命令:呼应字符串到一个文件中,然后重定向到命令窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个批处理文件来执行重复一个Fortran编译成可执行文件。通常我们会去到Windows命令提示符下,键入'Model.exe。这会弹出一个DOS命令窗口,询问用户在命令窗口在DOS提示符下直接键入所需的文件名。

I am trying to write a batch file to iteratively execute a fortran compiled executable. Normally one would go to the windows command prompt, type 'Model.exe'. This would bring up a dos command window asking the user to type a required file name directly in to the command window at the dos prompt.

我想编写一个批处理文件,将做到这一点对我来说,也重复这个步骤,这样我可以有手工做的运行10次仿真连续来代替。这种外壳的操作将是在linux简单,但我没有这个用。

I want to write a batch file that will do this bit for me, and also iterate this step so that I can run 10 simulations consecutively instead of having to do it by hand. This kind of shell operation would be straightforward in linux, but I do not have this available.

我的伪code是这样的:

My pseudo code would look like this:

为/ L %%运行(1,1,10)做的结果
(集海峡=辛%%运行结果
回声%STR%> input.txt的结果
Model.exe< input.txt中)

您可以在下面的步骤分解下来:

You could break this down in to the following steps:


  1. 分配变量'运行'的值。 (例如,1)

  2. 用字符串(SIM),并置此作一个新的变量,SIM1

  3. 此呼应到一个文本文件(input.txt中)

  4. 从文件input.txt中读取变量SIM1

  5. 可执行消失并做它的事情。

  6. 重复步骤1 - > 5,但有一个新的变量卡莱SIM2等

我可以得到上面的工作,如果我使用的集海峡= SIM1 的,然后这个回波直接为input.txt的,但我不能把这个作为一个循环工作。我缺少的东西吗?

I can get the above to work if I use set str=Sim1 and then echo this directly to "input.txt", but I cannot get this to work as a loop. Am I missing something?

最好的问候,

推荐答案

唉,CMD.EXE的处理变量扩展的是丑陋的。所以,你需要延迟扩张,内容如下:

Ugh, cmd.exe's treatment of variable expansion is ugly. So, you need "delayed expansion", as follows:

setlocal enabledelayedexpansion
for /L %%i in (1,1,10) do (
set str=Sim%%i
echo !str! > input.txt
Model.exe < input.txt)
endlocal

(当然,在这种特殊情况下,你可以只说回声辛%% I&GT; input.txt的但我相信有,为什么要通过去一个很好的理由另外一个变量。)

(Of course in this particular case you could just say echo Sim%%i > input.txt but I assume there's a good reason why you want to go via another variable.)

这篇关于多循环做一个命令:呼应字符串到一个文件中,然后重定向到命令窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆