2015-09-05 102 views
2

我在超时命令上出现语法错误,原因是“缺少操作数”。但我没有看到任何操作数丢失。带变量的批处理语句中缺少操作数错误

rem description for the user to read 
echo Insert time in minutes: 
rem setting default (1 hour) 
set /a timeto=3600 
rem asking user for input (integer) 
set /p timeto= 
rem converting minutes to seconds 
set /a timeto=%timeto%*60 
rem command based on the inputted value 
timeout /t %timeto% /nobreak 
+1

你不能以这种方式设置的意见。 – npocmaka

+0

我很新的批次。应如何设置评论? (顺便说一下,在我的文件中,我没有这些意见) – jony

回答

2

语法无效。在谈到命令行内是不可能的cmd CLI也不是一个批处理脚本:

==>set /a timeto=3600   ::setting default time (1 hour) 
Missing operator. 

==>set /a timeto=3600 
3600 
==> 

注意%timeTo%*60超过timeout /T有效范围-199999默认3600*60

如果您的代码段出现括在括号(),然后使用Delayed Expansion

@ECHO ON >NUL 
@SETLOCAL EnableExtensions EnableDelayedExpansion 
if 1==1 (
    rem description for the user to read 
    echo Insert time in minutes: 
    rem setting default time (1 hour = 60 minues) 
    set /a timeTo=60    
    rem asking user for input (integer) 
    set /p timeTo=     
    rem converting minutes to seconds - erroneous 
    set /a timeTo=%timeTo%*60  
    rem converting minutes to seconds - right approach 
    set /a timeTo=!timeTo!*60  
    rem command based on the inputted value 
    echo timeout /t !timeTo! /nobreak 
) 

输出:注意,set /a timeTo=%timeTo%*60线使Missing operand误差作为结果错误的set /a timeTo=*60在分析的时间。

==>D:\bat\SO\32410773.bat 

==>if 1 == 1 (
rem description for the user to read 
echo Insert time in minutes: 
rem setting default time (1 hour = 60 minues) 
set /a timeTo=60 
rem asking user for input (integer) 
set /p timeTo= 
rem converting minutes to seconds - erroneous 
set /a timeTo=*60 
rem converting minutes to seconds - right approach 
set /a timeTo=!timeTo!*60 
rem command based on the inputted value 
echo timeout /t !timeTo! /nobreak 
) 
Insert time in minutes: 

Missing operand. 
timeout /t 3600 /nobreak 

==> 
+0

我实际上没有测试默认值(已经用2分钟的延迟测试了它--120,所以一定不能是错误的原因)。无论如何,这确实是一个问题,我肯定会需要超过一个小时的超时。我该如何解决这个问题?是否有一些我可以使用的等待/睡眠声明? – jony

+1

'set/A timetomax = 99999/3600'返回27,因此最大。 “timeout/T 99999”的值等待超过27小时... – JosefZ

+0

我在看到对您的答案的编辑并理解您的解决方案之前编写了此评论。几个小时前我刚开始学习bash的基础知识,所以我仍然陷入困境,并对所有这些新概念感到吃惊。我理解这个解决方案(有点像使用PHP使用html)。感谢您的帮助。 – jony

1

批不支持嵌入式comments.All意见必须在单独一行:

::description for the user to read 
echo Insert time in minutes: 
::setting default time (1 hour) 
set /a timeto=3600 
::asking user for input (integer)    
set /p timeto= 
::converting minutes to seconds     
set /a timeto=%timeto%*60 
::command based on the inputted value  
timeout /t %timeto% /nobreak 
+0

更正了它,谢谢你的提示! – jony

+1

@jony - 在您编辑之后...检查JosefZ的答案。你需要延迟扩展并在括号内使用'!'而不是'''来访问变量。 – npocmaka

+0

是的,现在一切正常。我看到REM是另一种评论方式。 – jony