Batch

From Esolang
Jump to navigation Jump to search
Batch
Paradigm(s) imperative, unstructured
Designed by Microsoft, others
Appeared in 1983
Memory system variables, CLI arguments, files
Dimensions one-dimensional
Computational class Turing complete
Major implementations MS-DOS, Windows NT
Influenced by CP/M
Influenced !!!Batch, Numeric Batch, Restricted batch, Translated Batch, Batch No For, 'xxx' is not recognized as an internal or external command, operable program or batch file.
File extension(s) .bat, .cmd (NT)

Batch is a family of shell scripting languages developed by various companies, most notably Microsoft, to run multiple shell commands in order. Batch was primarily used for system configuration and startup, through the use of e.g. AUTOEXEC.BAT. Batch was first available as MS-DOS Batch in 1983 with the release of MS-DOS 2.0, although similar scripting languages predate it. The language was carried through into Windows and can still be used via cmd.exe.

Versions

Several companies have created systems intended to be compatible with MS-DOS. These implementations generally included Batch script functionality, as many programs used Batch scripts to configure applications, and many applications expected there to be an AUTOEXEC.BAT script to update on installation so that drivers and other utilities were configured properly on startup. There are also modern open source implementations of MS-DOS or Windows operating systems which provide Batch functionality.

Between various versions of the operating systems Batch is available on, the core syntax remains largely the same, instead Batch mainly changes through additions to the operating system, with additional utilities becoming available with new versions of e.g. MS-DOS and Windows.

Syntax

Batch is case insensitive. Later versions of Batch added support for blocks, which are segments of code surrounded by parentheses. Most commands are issued in the form command argument argument argument however, some have special syntax like IF and SET. Certain commands can accept flags, which are optional arguments prefixed with a character, typically /. Flags change the behavior of commands, and sometimes the syntax as well.

Features

Batch offers commands to set variables, perform conditional execution, define and jump to labels, handle CLI arguments, among others. Due to Batch's variable expansion rules, it is possible to write self-modifying code. The following example uses self modification to run either COMMAND (MS-DOS subshell) or WIN (Windows) depending on if an environment variable is set:

SET TORUN=WIN
IF "%USEWIN%" == "" SET TORUN=COMMAND
%TORUN%

The bare %TORUN% is expanded by the interpreter when the line is read. After expansion the interpreter parses it as a command and executes accordingly.

NT Batch introduced SETLOCAL which allows for the use of delayed expansion, among other things. Normally, Batch will replace variables when a line (or block) is parsed, but with delayed expansion they are instead replaced when encountered. Delayed expansion allows variables to be used as pointers.

SET A=1
SET B=2
SET PTR=A
ECHO A=%A% B=%B%
REM Set the value of A through the pointer
SET %PTR%=3
ECHO A=%A% B=%B%
REM Get the value of A through the pointer
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO *PTR=!%PTR%!

When the interpreter encounters the line ECHO *PTR=!%PTR%! it first replaces all %var% variables, resulting in ECHO *PTR=!A!. Once replaced, it executes the line (or block). As this block only contains one command it immediately executes the echo, and encounters the !A!. When encountered, it replaces it with the value of the variable, resulting in ECHO *PTR=3.

Windows 2000 and later extended the SET command with the /A flag to provide a more direct means of arithmetic.

Input is generally performed via the SET /P and CHOICE commands. These commands are not available on typical installations of MS-DOS, but there are additional command executables available for MS-DOS, as well as other versions of DOS which provide similar functionality.

Computational class

All versions of Batch developed by Microsoft are Turing complete, although their actual implementations are bounded-storage machines due to arbitrary limitations. The proof can be seen on the page for MS-DOS Batch, which holds for all Microsoft versions as the commands used in that proof are available in all subsequent versions of MS-DOS Batch, as well as NT Batch.

Example

Hello, world!

@echo off
echo Hello, world!
goto end
:end
pause

Scan your disk

@echo off
echo To check the disk, press Y; Otherwise, press N. To fix the disk, press F.
echo.
choice /c:ynf /d:y /t:30
if errorlevel 2 goto :skip
goto ch
:ch
echo What disk do you wanna scan?
echo.
choice /c:abcdefghijklmnopqrstuvwxyz0 /d:c /t:30
if errorlevel 27 goto :skip
if errorlevel 26 (
    scandisk Z:
    goto end
)
if errorlevel 25 (
    scandisk Y:
    goto end
)
if errorlevel 24 (
    scandisk X:
    goto end
)
if errorlevel 23 (
    scandisk W:
    goto end
)
if errorlevel 22 (
    scandisk V:
    goto end
)
if errorlevel 21 (
    scandisk U:
    goto end
)
if errorlevel 20 (
    scandisk T:
    goto end
)
if errorlevel 19 (
    scandisk S:
    goto end
)
if errorlevel 18 (
    scandisk R:
    goto end
)
if errorlevel 17 (
    scandisk Q:
    goto end
)
if errorlevel 16 (
    scandisk P:
    goto end
)
if errorlevel 15 (
    scandisk O:
    goto end
)
if errorlevel 14 (
    scandisk N:
    goto end
)
if errorlevel 13 (
    scandisk M:
    goto end
)
if errorlevel 12 (
    scandisk L:
    goto end
)
if errorlevel 11 (
    scandisk K:
    goto end
)
if errorlevel 10 (
    scandisk J:
    goto end
)
if errorlevel 9 (
    scandisk I:
    goto end
)
if errorlevel 8 (
    scandisk H:
    goto end
)
if errorlevel 7 (
    scandisk G:
    goto end
)
if errorlevel 6 (
    scandisk F:
    goto end
)
if errorlevel 5 (
    scandisk E:
    goto end
)
if errorlevel 4 (
    scandisk D:
    goto end
)
if errorlevel 3 (
    scandisk C:
    goto end
)
if errorlevel 2 (
    scandisk B:
    goto end
)
if errorlevel 1 (
    scandisk A:
    goto end
)
:skip
goto :end
:end
pause

External resources