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.

External resources