【问题标题】:Batch Script For cycle does not work循环的批处理脚本不起作用
【发布时间】:2017-05-25 20:57:39
【问题描述】:

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

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 IS NOT RECOGNIZED AS外部或内部命令

【问题讨论】:

  • 也许this 可以提供帮助
  • 好的。但似乎 y 变量没有更新
  • 您需要启用并应用delayed expansion,因为您正在编写在同一个带括号的代码块中读取变量...
  • SET /A 用于算术运算,不设置数值变量。你可以使用SET "y=1"set "max=16384"set "y=0"
  • 我从来没有说过它是无效的,我完全知道它的优点和缺点。在脚本的上下文和结构中,OP 不需要帮助和建议。

标签: windows batch-file


【解决方案1】:

您必须将您的 ELSE 子句与您的 IF-clause 操作的近括号放在同一行(我通常将其视为 ) 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
    )
  )
)

你也不需要/ASET将一个变量转换为一个简单的数字; SET /A 允许在 SET 命令中进行数学运算,例如,SET /A Y=%M%*4 将 Y 设置为 M 中存储的数字的四倍。

【讨论】:

  • 从代码的外观来看,未经测试,这里似乎不需要 else 。除了最后的else之外,只能使用IF ..
  • @GerhardBarnard - 不,没有ELSEs,如果 Y 以 1 开头,那么 Y=1 和 Y=0 子句都将被执行,因为在退出“then”块之后从第一次测试开始,它将测试 Y=0 - 届时将是真的。
  • 是的,刚刚注意到顶部的set /A=1
【解决方案2】:

正如其他答案/cmets 所示,您需要根据 C-K&R 样式放置右括号,而不是 Lisp 样式,如Indent Style Wikipedia 文章的大括号放置部分所示.

但是,您的代码过于复杂。您在第一个IF %y% EQU 1 中使用y 变量只是为了执行第一个ex 命令一次(顺便说一句,需要延迟扩展才能正常工作),而您使用最后一个if %%x LSS %max% 只是为了执行最后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

请注意,您也可以将多个命令括在括号中,并为它们全部使用一个重定向;这种方法不仅看起来更清晰,而且更高效/更快。

【讨论】:

  • 您好,我已经重写了代码,但是 step var 没有更新:inline SET /A max=16384, maxM1=8192, Nt=25, startM=16, M=1024, startNt=25, startNt1=startNt+1, maxNt=512, maxNt1=maxNt-1, step=32 SET "tol=1e-9" set "nfun=1" set "varpar=Mvar" ( ex %nfun% %tol% %startM% %Nt% %varpar% 1 FOR /L %%x IN (1,1,9) DO ( ex %nfun% %tol% %step% %Nt% %varpar% 0 set /A step = !step!*2 ) ex %nfun% %tol% %max% %Nt% %varpar% 2 ) > %OUTdir%\out_F01_times_9_M_var.txt endlocal
  • 当然。任何修改括号内的变量都需要!delayed_expansion!,exceptingset /A命令中! set /A step = step*2 操作也可以这样写:set /A step*=2;见set /?。总结:1. 在开头插入setlocal EnableDelayedExpansion2.FOR改成这个:FOR /L %%x IN (1,1,9) DO ( ex %nfun% %tol% !step! %Nt% %varpar% 0 set /A step*=2 )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多