Converting DOS Batch Files to Shell Scripts

Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting
Prev |  | Next

# Appendix N. Converting DOS Batch Files to Shell Scripts

Quite a number of programmers learned scripting on a PC running DOS. Even the crippled DOS batch file language allowed writing some fairly powerful scripts and applications, though they often required extensive kludges and workarounds. Occasionally, the need still arises to convert an old DOS batch file to a UNIX shell script. This is generally not difficult, as DOS batch file operators are only a limited subset of the equivalent shell scripting ones.

Table N-1. Batch file keywords / variables / operators, and their shell equivalents

Batch File Operator | Shell Script Equivalent | Meaning
--- | --- | ---
`%` | $ | command-line parameter prefix
`/` | - | command option flag
`\` | / | directory path separator
`==` | = | (equal-to) string comparison test
`!==!` | != | (not equal-to) string comparison test
`|` | | | pipe
`@` | set `+v` | do not echo current command
`*` | * | filename "wild card"
`>` | > | file redirection (overwrite)
`>>` | >> | file redirection (append)
`<` | < | redirect `stdin`
`%VAR%` | $VAR | environmental variable
`REM` | # | comment
`NOT` | ! | negate following test
`NUL` | `/dev/null` | "black hole" for burying command output
`ECHO` | echo | echo (many more option in Bash)
`ECHO.` | echo | echo blank line
`ECHO OFF` | set `+v` | do not echo command(s) following
`FOR %%VAR IN (LIST) DO` | for var in [list]; do | "for" loop
`:LABEL` | none (unnecessary) | label
`GOTO` | none (use a function) | jump to another location in the script
`PAUSE` | sleep | pause or wait an interval
`CHOICE` | case or select | menu choice
`IF` | if | if-test
`IF EXIST `FILENAME`` | if [ -e filename ] | test if file exists
`IF !%N==!` | if [ -z "$N" ] | if replaceable parameter "N" not present
`CALL` | source or . (dot operator) | "include" another script
`COMMAND /C` | source or . (dot operator) | "include" another script (same as CALL)
`SET` | export | set an environmental variable
`SHIFT` | shift | left shift command-line argument list
`SGN` | -lt or -gt | sign (of integer)
`ERRORLEVEL` | $? | exit status
`CON` | `stdin` | "console" (`stdin`)
`PRN` | `/dev/lp0` | (generic) printer device
`LPT1` | `/dev/lp0` | first printer device
`COM1` | `/dev/ttyS0` | first serial port

Batch files usually contain DOS commands. These must be translated into their UNIX equivalents in order to convert a batch file into a shell script.

Table N-2. DOS commands and their UNIX equivalents

DOS Command | UNIX Equivalent | Effect
--- | --- | ---
`ASSIGN` | ln | link file or directory
`ATTRIB` | chmod | change file permissions
`CD` | cd | change directory
`CHDIR` | cd | change directory
`CLS` | clear | clear screen
`COMP` | diff, comm, cmp | file compare
`COPY` | cp | file copy
`Ctl-C` | Ctl-C | break (signal)
`Ctl-Z` | Ctl-D | EOF (end-of-file)
`DEL` | rm | delete file(s)
`DELTREE` | rm -rf | delete directory recursively
`DIR` | ls -l | directory listing
`ERASE` | rm | delete file(s)
`EXIT` | exit | exit current process
`FC` | comm, cmp | file compare
`FIND` | grep | find strings in files
`MD` | mkdir | make directory
`MKDIR` | mkdir | make directory
`MORE` | more | text file paging filter
`MOVE` | mv | move
`PATH` | $PATH | path to executables
`REN` | mv | rename (move)
`RENAME` | mv | rename (move)
`RD` | rmdir | remove directory
`RMDIR` | rmdir | remove directory
`SORT` | sort | sort file
`TIME` | date | display system time
`TYPE` | cat | output file to `stdout`
`XCOPY` | cp | (extended) file copy

| Virtually all UNIX and shell operators and commands have many more options and enhancements than their DOS and batch file counterparts. Many DOS batch files rely on auxiliary utilities, such as ask.com, a crippled counterpart to read. DOS supports only a very limited and incompatible subset of filename wild-card expansion, recognizing just the * and ? characters.

Converting a DOS batch file into a shell script is generally straightforward, and the result ofttimes reads better than the original.

Example N-1. VIEWDATA.BAT: DOS Batch File

       1 REM VIEWDATA
       2
       3 REM INSPIRED BY AN EXAMPLE IN "DOS POWERTOOLS"
       4 REM                           BY PAUL SOMERSON
       5
       6
       7 @ECHO OFF
       8
       9 IF !%1==! GOTO VIEWDATA
      10 REM  IF NO COMMAND-LINE ARG...
      11 FIND "%1" C:\BOZO\BOOKLIST.TXT
      12 GOTO EXIT0
      13 REM  PRINT LINE WITH STRING MATCH, THEN EXIT.
      14
      15 :VIEWDATA
      16 TYPE C:\BOZO\BOOKLIST.TXT | MORE
      17 REM  SHOW ENTIRE FILE, 1 PAGE AT A TIME.
      18
      19 :EXIT0

The script conversion is somewhat of an improvement. [1]

Example N-2. viewdata.sh: Shell Script Conversion of VIEWDATA.BAT

       1 #!/bin/bash
       2 # viewdata.sh
       3 # Conversion of VIEWDATA.BAT to shell script.
       4
       5 DATAFILE=/home/bozo/datafiles/book-collection.data
       6 ARGNO=1
       7
       8 # @ECHO OFF                 Command unnecessary here.
       9
      10 if [ $# -lt "$ARGNO" ]    # IF !%1==! GOTO VIEWDATA
      11 then
      12   less $DATAFILE          # TYPE C:\MYDIR\BOOKLIST.TXT | MORE
      13 else
      14   grep "$1" $DATAFILE     # FIND "%1" C:\MYDIR\BOOKLIST.TXT
      15 fi
      16
      17 exit 0                    # :EXIT0
      18
      19 #  GOTOs, labels, smoke-and-mirrors, and flimflam unnecessary.
      20 #  The converted script is short, sweet, and clean,
      21 #+ which is more than can be said for the original.

Ted Davis' Shell Scripts on the PC site has a set of comprehensive tutorials on the old-fashioned art of batch file programming. Certain of his ingenious techniques could conceivably have relevance for shell scripts.

### Notes

[1] | Various readers have suggested modifications of the above batch file to prettify it and make it more compact and efficient. In the opinion of the ABS Guide author, this is wasted effort. A Bash script can access a DOS filesystem, or even an NTFS partition (with the help of ntfs-3g) to do batch or scripted operations.

Prev | Home | Next
Sample `.bashrc` and `.bash_profile` Files |  | Exercises