You are not logged in.
Pages: 1
Hi all,
While writing one CONTROL script, I found that when I read a nul text file, 1 it returns some non-printable
character and because of that the script fails
:KEEPLOOPING
set STATE=
FOR /F %%X IN (%CTRLFILE%) DO SET STATE=%%X
IF NOT [%STATE%]==[] (
Type %CTRLFILE% >>%HISTORY%
Type %MSGFILE% >>%HISTORY%
Type Nul> %CTRLFILE%
Type Nul> %MSGFILE%
)
call :DELAY 2 2000
:
:
:
{ BRANCHING CODE }
.
While the CTRLFILE is supposed to be nul most of the time; it will get 3 letter of control code for the script to read and branch accordingly.
The script fails at IF after reading nul file with FOR
Note that it works fine if I keep more than one line in CTRLFILE. But my requirement is to keep it nul till I type the command to divert the script. ( commnads like TRM CON EXT REP or even one digit )
Soon a command is entered it is read and is transfered to history file before script does anything. The CTRLFILE is left nul again.
It seems that FOR reads either EOF char or EOL chars when file is nul.
D:\GCPROJ>echo [%STATE%]==[]
[ ]==[]
And the error at runtime
D:\GCPROJ>ctr
Waiting on Command
]==[] was unexpected at this time.
Any work around
Thanks
Sarbjit Singh Gill
Last edited by sarbjitsinghgill (21 Apr 2009 20:21)
Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer
Offline
Is there white space after set STATE= in the 3rd line of the fragment above? It certainly looks like there is when I select the text in my browser.
Try changing the line to set "STATE="
An editor that can display white space visibly (and always, always, always wrapping SET commands in quotes) will help prevent things like that.
Also I generally don't recommend using [ ] in "IF" statements as you have done. Wrapping in double quotes means stray spaces and special characters are much less likely to cause random script crashes.
Last edited by bluesxman (21 Apr 2009 23:39)
cmd | *sh | ruby | chef
Offline
Thanks bluesxman,
You are really great.
This logic works and I need to know what kind of dely or sleep will be good for this logic code.
I am using counting numbers in nested for loops to slow down while processing commands while pausing.
I mean something that may slow down iteration while looping to save CPU time.
LOGIC
My script will run for hours and I have sub devided it in 6 sections. My manager may want to suspend it or hold it for a while or he may want to terminate while it is in section 2 only. He may be able to skip next section realizing that section 3 is not really needed at this time.
Creating CTRLFILE in script's working directory is enough to pause it ; or with HLD keyword. the script will put response in MSGEFILE and manager will interact with script this way.
CTRL file will also be useful to indicate that I want to run section 4 only. For this I will start script with 4 in STATEFILE and TRM in CTRLFILE.
@echo off
set STATEFILE=STATE.TXT
Set MSGFILE=MSG.TXT
Set CTRLFILE=CTRL.TXT
Set HISTORY=HSTRY.TXT
::
::
::
Set "State=1"
if exist %CTRLFILE% call :CTRLLOOP
echo.%State%>%STATEFILE%
goto State%STATE%
:State1
::
::
::
::
Set "State=2"
if exist %CTRLFILE% call :CTRLLOOP
echo.%State%>%STATEFILE%
goto State%STATE%
:State2
::
::
::
::
Set "State=3"
if exist %CTRLFILE% call :CTRLLOOP
echo.%State%>%STATEFILE%
goto State%STATE%
:State3
::
::
::
::
Set "State=4"
if exist %CTRLFILE% call :CTRLLOOP
goto State%STATE%
:State4
::
::
::
::
Set "State=5"
if exist %CTRLFILE% call :CTRLLOOP
echo.%State%>%STATEFILE%
goto State%STATE%
:State5
::
::
::
::
Set "State=6"
if exist %CTRLFILE% call :CTRLLOOP
echo.%State%>%STATEFILE%
goto State%STATE%
:State6
::
::
::
::
goto :StateEXIT
:CTRLLOOP
ECHO Waiting on Command >%MSGFILE%
TYPE %MSGFILE%
:: Save State of Script Execution
set "PS=%STATE%"
:KEEPLOOPING
set "State="
if exist %CTRLFILE% ( for /F %%m in (ctrl.txt) do set "State=%%m"
) else ( set "State=%PS%"
GOTO :EOF
)
call :DELAY 2 2000
IF NOT [%STATE%]==[] (
Type %MSGFILE% >>%HISTORY%
Type %CTRLFILE% >>%HISTORY%
echo.> %CTRLFILE%
echo.> %MSGFILE%
)
IF [%STATE%]==[TRM] ( Set "State=EXIT"
echo. Terminate Commanded>>%MSGFILE%
GOTO :EOF
)
IF [%STATE%]==[CON] ( Set "State=%PS%"
echo. Continuing From %PS%>>%MSGFILE%
GOTO :EOF
)
IF [%STATE%]==[HLO] ( echo HELLO
echo. Saying Hello>>%MSGFILE%
)
GOTO :KEEPLOOPING
:DELAY
for /l %%i in (1,1,%1) do ( echo %%i>nul
for /l %%j in (1,1,%2) do echo %%j>nul
)
goto :eof
:StateEXIT
@echo on
Can you suggest me something that will slow the iteration with relieving CPU not using extensively.
Thanks
Sarbjit Singh Gill
PS: above code is just a structural code. I will use it in a .bat file . Purely .bat script.
Last edited by sarbjitsinghgill (23 Apr 2009 01:03)
Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer
Offline
I use variations on this theme to "sleep" (this example sleeps ~1 second):
ping 127.1 -n 2 >nul
Make sure you leave a space before the ">".
Sub-procedures to sleep for an arbitrary number of seconds could look like this:
@echo off
call :sleep 3
call :sleep.display 10
pause
goto :EOF
:sleep
set "packets=1"
set /a "packets+=%1"
echo:Sleeping for %1 seconds...
ping 127.1 -n %packets% >nul
goto :EOF
:sleep.display
set "erase="
for /l %%a in (%1,-1,1) do (
set /p "out=%erase%%erase:= %%erase%Sleeping [%%a]" <nul
ping 127.1 -n 2 >nul 2>&1
)
echo:
goto :EOF
Last edited by bluesxman (23 Apr 2009 13:05)
cmd | *sh | ruby | chef
Offline
I like the display version of that.
Here's a script I wrote:
wait.cmd
:: Version 0.1
@setlocal
@if 1%1 == 1 goto error
@echo.%1|findstr /r [^^0-9]>nul 2>&1
@if not errorlevel 1 goto error
@set sec=%1
@if not 1%sec:~0,-10%==1 goto error
@if 1%sec:~0,-9% GTR 12 goto error
@if 1%sec:~0,-9% EQU 12 (
@if 1%sec:~1,9% GTR 1147483646 goto error
)
@if %sec% EQU 0 endlocal && goto :eof
@set /a sec+=1
@if %1 GTR 1 (
@echo Waiting %1 seconds.
) else (
@echo Waiting %1 second.
)
@ping -w 999 -n %sec% 127.0.0.1 >nul 2>&1
@endlocal && goto :eof
:error
@echo wait takes a numeric argument between
@echo 1 and 2147483646, inclusive, representing
@echo the number of seconds you want to wait.
@echo The time is not particularly precise.
@endlocal && goto :eof
Offline
Thanks
bluesxman
and
avery_larry
Both codes fit perfectly in my script. I too like the Display version. But in my script there will be no display.
I will use 2 second delay in my iterations.
Sarbjit Singh Gill
Last edited by sarbjitsinghgill (24 Apr 2009 00:11)
Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer
Offline
Pages: 1