2016-12-07 109 views
0

我的批处理脚本在第二个if状态:nbig中失败。 (我发现每个陈述后都使用@echo)。但是,if中的SET语句如果正常运行,则会成功,因为if语句中的@echo也是如此。这是非常奇特的,我看不出我做错了什么。批处理脚本在IF语句中失败

我的代码是在这里:

:: Set the day and night values 
set /A nighttemp = 2700 
set /A daytemp = 6500 
:: Set Transition Duration 
set /A transitionduration = 60 
:: Set times in minutes from midnight 
set /A tnight = 1380 
set /A tday = 480 

For /f "tokens=1-4 delims=/:." %%a in ("%TIME%") do (
    SET /A HH24=%%a 
    SET /A MI=%%b 
    SET /A SS=%%c 
    SET /A FF=%%d 
) 

SET /A mins = %HH24%*60 + %MI% 
SET /A tdaywindow = %tday% + 60 
SET /A tnightwindow = %tnight% + 60 

if %tnight% GEQ %tday% (GOTO NBIG) 
if %tnight% LSS %tday% (GOTO DBIG) 

pause 

:NBIG 

if %mins% LSS %tday% (SET /A temp = %nighttemp%) 
if %mins% LSS %tdaywindow% (SET /A temp = (%daytemp% - %nighttemp%)*((%mins% - %tday%)/60) + %nighttemp%) 
if %mins% LSS %tnight%(SET /A temp = %daytemp%) 
if %mins% LSS %tnightwindow%(SET /A temp = (%nighttemp% - %daytemp%)*((%mins% - %tnight%)/60) + %daytemp%) 
GOTO ENDING 

:DBIG 
if %mins% LSS %tnight%(SET /A temp = %daytemp%) 
if %mins% LSS %tnightwindow% (SET /A temp = (%nighttemp% - %daytemp%)*((%mins% - %tnight%)/60) + %daytemp%) 
if %mins% LSS %tday% (SET /A temp = %nighttemp%) 
if %mins% LSS %tdaywindow% (SET /A temp = (%daytemp% - %nighttemp%)*((%mins% - %tday%)/60) + %nighttemp%) 
GOTO ENDING 

:ENDING 
@echo %temp% 
pause 

::%~dp0\redshift.exe -O %temp% 

一个运行正常的程序应该@echotemp价值,但它的错误。

(旁白:这是运行与自定义时间红移程序...)

+1

请阅读栈简化代码溢出文档页面[批处理文件中的变量](http://stackoverflow.com/documentation/batch-file/3528/)。 – Mofi

回答

2

中SET/A是批处理解析器必须解析SET/A命令之前,打开和关闭括号,所以关闭括号SET/A在之前应用于开口零件

你要么需要计算

if %mins% LSS %tdaywindow% (SET /A temp = (%daytemp% - %nighttemp%^)*((%mins% - %tday%^)/60^) + %nighttemp%) 

或在逃避关闭括号更好,但引号内的整个分配:

if %mins% LSS %tdaywindow% (SET /A "temp = (%daytemp% - %nighttemp%)*((%mins% - %tday%)/60) + %nighttemp%") 

但也许是最简单的解决办法是沟外面的东西完全是你的代码构造方式所不需要的。

if %mins% LSS %tdaywindow% SET /A "temp = (%daytemp% - %nighttemp%)*((%mins% - %tday%)/60) + %nighttemp%" 

即使他们不需要这最后一种形式,我仍然想把我的SET分配放在引号中。

无关你的错误,你还可以通过使用SET/A功能,可自动扩展的数值变量,而无需%!(仅适用于SET/A)

if %mins% LSS %tdaywindow% SET /A "temp = (daytemp - nighttemp)*((mins - tday)/60) + nighttemp"