2016-12-07 99 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"