批处理脚本循环不起作用

时间:2017-05-25 09:57:01

标签: windows batch-file

你好我这个批处理脚本有问题,它不起作用:

set OUTdir=../output

@echo on
:: ex  nFun  tol  M Nt print
::     nFun: numero di funzione test [1,...,7]
::     tol:  error tolerance
::     M:    ridotta di ordine 2*M+1
::     Nt:   Numero di valori della funzione inversa
::     print: 1 (to print the header) 2(to print the end of the header) 0 or nothing( to print just output values)

SET /A y=1
set /A max=16384

FOR /L %%x IN (32,1,%max%) DO (
 IF  %y% EQU 1 (
  ex 1 1e-9 %%x 25  %y% > %OUTdir%\out_F01_times_9.txt   
  set /A y=0) 
  ELSE (
  if  %y% EQU 0 if  %%x LSS %max% (  
    ex 1 1e-9 %%x 25 0  > %OUTdir%\out_F01_times_9.txt )
   ELSE (
    ex 1 1e-9 %%x 25 2  > %OUTdir%\out_F01_times_9.txt ) )
)

ex是一个程序,其他数字是命令行的参数。

你可以帮帮我吗?我正在尝试创建一个批处理来执行具有不同参数的ex程序。

错误是ELSE未被识别为外部或内部命令

2 个答案:

答案 0 :(得分:0)

您必须将ELSE条款放在与IF - 子句操作的近距离相同的行上(我最常将其视为) ELSE (一行) :

set OUTdir=../output

@echo on
:: ex  nFun  tol  M Nt print
::     nFun: numero di funzione test [1,...,7]
::     tol:  error tolerance
::     M:    ridotta di ordine 2*M+1
::     Nt:   Numero di valori della funzione inversa
::     print: 1 (to print the header) 2(to print the end of the header) 0 or nothing( to print just output values)

SET /A y=1
set /A max=16384

FOR /L %%x IN (32,1,%max%) DO (
 IF  %y% EQU 1 (
  ex 1 1e-9 %%x 25  %y% > %OUTdir%\out_F01_times_9.txt   
  set /A y=0 
  ) ELSE (
  if  %y% EQU 0 if  %%x LSS %max% (  
    ex 1 1e-9 %%x 25 0  > %OUTdir%\out_F01_times_9.txt
    ) ELSE (
    ex 1 1e-9 %%x 25 2  > %OUTdir%\out_F01_times_9.txt
    )
  )
)

/A变量为简单数字时,您也不需要SET; SET /A允许SET命令中的数学运算,例如SET /A Y=%M%*4,将Y设置为M中存储的数字的四倍。

答案 1 :(得分:0)

正如其他答案/评论所示,您需要将右括号相应地放置在C-K& R样式中, Lisp样式,如{{3的大括号放置部分所示维基百科文章。

但是,您的代码过于复杂。您正在使用第一个y中的IF %y% EQU 1变量来执行第一个ex命令(BTW,要求延迟扩展才能正常工作),并且您正在使用最后一个if %%x LSS %max% {1}}只执行最后一次ex命令。您可以使用此代码获得相同的结果,不需要任何if,因此它应该运行得更快:

set OUTdir=../output


SET /A max=16384, maxM1=max-1

(  ex 1 1e-9 32 25 1
   FOR /L %%x IN (33,1,%maxM1%) DO (
      ex 1 1e-9 %%x 25 0
   )
   ex 1 1e-9 %max% 25 2
) > %OUTdir%\out_F01_times_9.txt

请注意,您也可以在括号中包含几个命令,并且只使用一个重定向;这种方法不仅看起来更清晰,而且效率更高/更快。