3 ms·
DOS/Windows Batch files behave same way. COMMAND.COM rereads whole batch file every time it executes one command line. PING localhost -n 10 #echo nothi
by rasz 7y ago
DOS/Windows Batch files behave same way. COMMAND.COM rereads whole batch file every time it executes one command line.
PING localhost -n 10
#echo nothing to see here
echo finish
D:\123>PING localhost -n 10
Pinging ... [127.0.0.1] with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
...
Ping statistics for 127.0.0.1:
Packets: Sent = 10, Received = 10, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
D:\123>#echo nothing to see here
'#echo' is not recognized as an internal or external command,
operable program or batch file.
D:\123>echo finish
finish
now rerun it and edit first line to 'PING localhost -n 1' and suddenly
...
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
D:\123>echo nothing to see here
nothing to see here
D:\123>echo finish
finish
- weinzierl 7y agoWhat you describe is also interesting behaviour but it is different from the effect seen in bash. For bash it works rougly like this (discounting the shebang to keep things simple): 1. bash reads first line and remembers the byte offset where to continue next. In this case at the octothorpe at byte position 10 (assuming UNIX line endings). 2. bash executes first line 3. while bash is still busy with the first line someone removes a character before position 10 4. Bash reads what it assumes to be the next line at byte position 10, which is now where the 'r' is. 5. bash executes 'rm -rf --no-preserve-root' EDIT: The observed behaviour for a DOS/Windows batch file is indeed the same. I assumed that the trick would not work because supposedly Windows reads the whole file again after every command, but apparently it does not remember the line it was executing but also a byte offset echo start pause echo stop Removing the last character from the first line while the script is paused results in Bad command or filename - "cho".
- JdeBP 7y agoThis is why Rex Conn's add-on command interpreter for MS/PC/DR-DOS, 4DOS, had the concept of BTM scripts. BTM stands for "Batch To Memory" and 4DOS (and all of its successors through NDOS, 4NT, and 4OS2 to the current Take Command) reads the entirety of a BTM script before running it. * https://jpsoft.com/help/batchtype.htm https://jpsoft.com/help/batchtype.htm The reason that this could not be simply turned on globally for BAT scripts is that various tools made use of the original behaviour, perhaps most notably "fancy change directory" scripts and similar programs, where a wrapped executable was called from a wrapper script, and the executable rewrote the next lines of the script on the fly to do the selected action.