Classic Computer Magazine Archive COMPUTE! ISSUE 59 / APRIL 1985 / PAGE 127

IBM BASIC's Undocumented
SHELL Command

Michael A. Covington

With DOS 3.0, IBM has announced a number of new features for disk BASIC. At least one of them' is actually present in DOS 2.0 and 2.1 as well, though the manuals do not mention it. That feature is a command called SHELL that allows you to execute DOS commands from within BASIC. (The technique does not work with PCjr Cartridge BASIC.)

The SHELL command in IBM BASIC takes one parameter, a character string containing the DOS command to be executed. SHELL works by loading, from drive A, a second copy of COMMAND.COM (the DOS command processor) and invoking it as a subprocess. (Note that this implies that COMMAND.COM must be present on the disk in drive A when the SHELL command is executed.) The top level COMMAND.COM and the BASIC interpreter are in suspended animation until the subprocess finishes; then control returns to BASIC.
    SHELL handles the cursor somewhat awkwardly. When the SHELL command is executed, the screen is cleared from the current cursor position to the bottom; DOS writes its output there, scrolling as needed (the twenty-fifth line scrolls along with the others). But when control returns to BASIC, the cursor suddenly appears one line below where it was when the subprocess started, ignoring all screen activity that took place under the subprocess.
    The best way to prevent chaos on the screen is to execute a CLS (clear-screen) immediately after each SHELL, or as soon afterward as you're done looking at the output.

Not A Child
The one command that SHELL cannot issue, either directly or indirectly, is BASIC (or BASICA). If you try to do this, you get the message "You cannot run Basic as a Child of Basic"-naturally enough, you can't run BASIC in the subprocess because most of BASIC is in ROM and there's only one copy of it in the machine. If you issue a SHELL and COMMAND.COM is not on drive A, you get a "File not found" error within BASIC.
    The most useful SHELL commands are probably:

    SHELL "A:"
    SHELL "B:"

and the like, to change logged disks. These are foolproof commands; they produce no messages to clutter up the screen, and they can't terminate abnormally.
    You can also use SHELL without parameters, in immediate mode, to enter the DOS command mode. The advantage of this over SYSTEM is that when you're done issuing DOS commands, you can type EXIT and return to BASIC with your program undisturbed.
    Most kinds of errors in the subprocess will return you to BASIC with no problem, but a few, such as typing A in response to "Abort, Retry, Ignore," will leave you in the DOS command level of the subprocess, in which case you must type EXIT to get back to BASIC.

One At A Time
Don't issue several SHELL commands in succession if you can avoid it; each of them loads COMMAND.COM all over again. Instead, if you have a series of commands to issue, write them onto a BAT file from within BASIC, and give one command to run the whole file.
    The accompanying program demonstrates one way to use SHELL to create a menu-driven user interface for DOS. Naturally, a practical program would include many more options and more error-checking.

Executes a DOS command from within BASIC. This is done by loading a second copy of COMMAND.COM and invoking it as a subprocess.
Cassette Disk Advanced Compiler
Format: SHELL    or    SHELL X$
Remarks: X$ is a character string constant, variable, or expression containing any valid DOS command.

In order for SHELL to work, COMMAND.COM must be present on disk A. If it is not, the message "File not found" is displayed.

X$ can be an internal DOS command or invoke a .COM,.EXE, or .BAT file. However, the BASIC interpreter cannot be invoked using SHELL; if this is attempted, the message "You cannot run Basic as a Child of Basic" is displayed.

The amount of memory available in the subprocess is markedly less than is available in DOS by itself.

If X$ is omitted, the user is placed at the DOS command level of the subprocess. To return to the calling BASIC program, type the command EXIT.

Certain fatal errors in the subprocess may also leave the user at the DOS command level of the subprocess; again, typing EXIT returns control to BASIC. However, most errors in the subprocess return control to the calling BASIC program automatically.
(to go temporarily into command mode)
SHELL "B:" (to change logged disk)
SHELL "MYFIL" (to invoke
MYFIL.EXE, as the case may be)

Demo of SHELL Command

GI 10 ' COMMAND.COM must be on drive A
1A 20 ' MORE.COM and CHKDSK.COM must be
ME 30 '    on the current default disk
BD 50 PRINT "Welcome to menu-driven DOS."
LO 70 PRINT "Available functions are:"
NG 80 PRINT " 1 Directory of disk A"
AG 90 PRINT " 2 Directory of disk B"
OJ 100 PRINT " 3 Disk and memory inform
BD 110 PRINT " 4 Copy a file"
AB 120 PRINT " 5 View a file"
JK 130 PRINT " 6 End this program"
J1 140 PRINT
KA 150 INPUT "Choose one...";N
OF 170 IF (N<1) OR (N>5) THEN BEEP: GOTO
BN 180 CLS
KA 190 ON N GOTO 210,240,270,320,370
MD 200 ' directory of A
AN 210 SHELL "dir a:"
CP 220 GOTO 400
ON 230 ' directory of B
CN 240 SHELL "dir b:"
CF 250 GOTO 400
OG 260 ' disk & memory info.
CG 270 INPUT "Drive to check ";A$
BI 280 IF A$="a"OR A$="A" THEN SHELL "chk
       dsk a:"
FG 290 IF A$="b"OR A$="B" THEN SHELL "chk
       dsk b:"
CM 300 GOTO 400
ML 310 ' copy a file
MC 320 INPUT "File to copy from ";A$
CM 330 INPUT "File to copy onto ";B$
EO 340 SHELL "copy "+A$+" "+B$
CG 350 GOTO 400
KD 360 ' view a file
OA 370 INPUT "Name of file ";A$
CG 380 SHELL "more <"+A$
LJ 390 ' finish up
DN 400 LOCATE 25,1
OB 420 PRINT "(Press any key to continue.
OF 440 GOTO 40