Random ECHO is Off. in numbers

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rasil
Posts: 31
Joined: 23 Apr 2020 13:05

Random ECHO is Off. in numbers

#1 Post by rasil » 08 Jul 2021 15:22

Hello,

Here i have a simple program that makes 3 lots of really random numbers.

Code: Select all

@echo off
rem ------------------------------------------------
rem component of project not to be used seperately.
rem ------------------------------------------------
rem 36x10 hex components
rem [Default 30000, 1] [Max 30000, 1]
rem start value
set numst=1
rem end value
set numen=36
set count=0
:clock
rem internal clock
if %count%==0 set count=1&goto ranumpasshorizantal
if %count%==1 set count=2&goto ranumpasshorizantal
if %count%==2 set count=3&goto ranumpasshorizantal
:ranumpasshorizantal
SET /A ranumche=%RANDOM% * %numen% / 32768 + %numst%&echo +1
SET /A ranum=%RANDOM% * %numen% / 32768 + %numst%&echo +1
if %ranumche%==%ranum% goto ranumpasshorizantal
:ranumpasshorizantal1
SET /A ranumche1=%RANDOM% * %numen% / 32768 + %numst%&echo +1
SET /A ranum1=%RANDOM% * %numen% / 32768 + %numst%&echo +1
if %ranumche1%==%ranum1% goto ranumpasshorizantal1
if %ranum1%==%ranum% goto ranumpasshorizantal
echo %ranum%>>horizontal.hextmp
echo (%count%)
if %count%==3 goto horizontalmapok
goto clock
:horizontalmapok
set count=0
echo.
type horizontal.hextmp
del horizontal.hextmp
pause>nul
But sometimes it shows a ECHO is Off. 2 or 3 times.
The console looks something like this:
Image
The file horizontal.hextmp looks something like this:
Image
Have a good day :wink:

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Random ECHO is Off. in numbers

#2 Post by aGerman » 08 Jul 2021 16:21

There are a couple of things which should be considered as best practice. One of them will prevent your error.

1) Always quote paths and file names.

Code: Select all

type foo.txt
works,

Code: Select all

type foo bar.txt
fails.

Code: Select all

type "foo bar.txt"
works, and quoting also works for file names without spaces or special characters like &.


2) Always quote assignments.

Code: Select all

set foo=bar
works

Code: Select all

set foo=bar&baz
fails.

Code: Select all

set "foo=bar&baz"
works. An so does

Code: Select all

set /a "num=1|2"
which would fail without quoting.


3) Always write the redirection first. (your current error)

Code: Select all

echo 12345>"foo.txt"
works

Code: Select all

echo 1>"foo.txt"
fails because single digits are taken as stream identifiers.

Code: Select all

>"foo.txt" echo 1
works.


4) Quote comparison operands when performing string comparisons (rather than numeric comparisons).
If a comparison operand contains spaces or special characters or if it is an undefined variable, the script fails with a syntax error.

Code: Select all

if /i "abc def"=="ABC DEF" echo Same
NOTE: The quotation marks belong to the compared string though.


5) If you have to write an empty line or you don't know whether the string to be printed is empty or consists of only spaces, use a left parenthesis adjacent to the ECHO command.

Code: Select all

set "spaces=          "
echo(%spaces%

There are a couple more. However, these should already protect you from most pitfalls.

Steffen

findstr
Posts: 17
Joined: 09 Jun 2021 12:36

Re: Random ECHO is Off. in numbers

#3 Post by findstr » 09 Jul 2021 13:55

aGerman wrote:
08 Jul 2021 16:21

There are a couple more. However, these should already protect you from most pitfalls.
Can you please provide the information about those too?
Thanks!

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Random ECHO is Off. in numbers

#4 Post by aGerman » 10 Jul 2021 04:12

Hmm, OK. Maybe two more worth to mention.

5) When processing unknown content, keep in mind that assigning a variable is safe in an environment with disabled delayed variable expansion while working with the value is safe with delayed variable expansion enabled.

Code: Select all

@echo off

setlocal DisableDelayedExpansion
set "str=The teacher angry: Shut up! Be quiet & listen to me!"

echo %str%

pause
fails to output the string due to the &.

Code: Select all

@echo off

setlocal EnableDelayedExpansion
set "str=The teacher angry: Shut up! Be quiet & listen to me!"

echo !str!

pause
fails to define the variable due to the expansion of the (undefined) variable represented by the substring between the two exclamation points.

Code: Select all

@echo off

setlocal DisableDelayedExpansion
set "str=The teacher angry: Shut up! Be quiet & listen to me!"
setlocal EnableDelayedExpansion
echo !str!

pause
works.
Note: You can't endlessly stack new sub-environments using SETLOCAL. Especially if you do this in a loop the maximum depth is reached quickly. Use ENDLOCAL to release created sub-environments.



6) Avoid GOTOs whenever possible (and 99% of the times it is possible). You may need it to implement a WHILE loop. You may use the special case GOTO :EOF to quit a script or a subroutine. You may use it to quickly escape out of loops. However, you definitly never need it for branching. Don't jump in the wild! Nobody but you will be able to follow the program flow. And not even you will be able after four weeks not looking at the code.
Instead use subroutines and CALL :Label. It's always clear that the program flow will continue at the point the subroutine was called.

Steffen

Post Reply