当前位置: 首页 > 文档资料 > Shell 中文文档 >

Appendix L. Converting DOS Batch Files to Shell Scripts

优质
小牛编辑
127浏览
2023-12-01

Quite a number of programmers learned scripting on a PC runningDOS. Even the crippled DOS batch file language allowed writing somefairly powerful scripts and applications, though they often requiredextensive kludges and workarounds. Occasionally, the need stillarises to convert an old DOS batch file to a UNIX shell script. Thisis generally not difficult, as DOS batch file operators are only alimited subset of the equivalent shell scripting ones.


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

Batch File OperatorShell Script EquivalentMeaning
%$command-line parameter prefix
/-command option flag
\/directory path separator
===(equal-to) string comparison test
!==!!=(not equal-to) string comparison test
||pipe
@set +vdo not echo current command
**filename "wild card"
>>file redirection (overwrite)
>>>>file redirection (append)
<<redirect stdin
%VAR%$VARenvironmental variable
REM#comment
NOT!negate following test
NUL/dev/null"black hole"for burying command output
ECHOechoecho (many more option in Bash)
ECHO.echoecho blank line
ECHO OFFset +vdo not echo command(s) following
FOR %%VAR IN (LIST) DOfor var in [list]; do"for"loop
:LABELnone (unnecessary)label
GOTOnone (use a function)jump to another location in the script
PAUSEsleeppause or wait an interval
CHOICEcase or selectmenu choice
IFifif-test
IF EXIST FILENAMEif [ -e filename ]test if file exists
IF !%N==!if [ -z "$N" ]if replaceable parameter "N"not present
CALLsource or . (dot operator)"include"another script
COMMAND /Csource or . (dot operator)"include"another script (same asCALL)
SETexportset an environmental variable
SHIFTshiftleft shift command-line argument list
SGN-lt or -gtsign (of integer)
ERRORLEVEL$?exit status
CONstdin"console"(stdin)
PRN/dev/lp0(generic) printer device
LPT1/dev/lp0first printer device
COM1/dev/ttyS0first serial port

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


Table L-2. DOS commands and their UNIX equivalents

DOS CommandUNIX EquivalentEffect
ASSIGNlnlink file or directory
ATTRIBchmodchange file permissions
CDcdchange directory
CHDIRcdchange directory
CLSclearclear screen
COMPdiff, comm, cmpfile compare
COPYcpfile copy
Ctl-CCtl-Cbreak (signal)
Ctl-ZCtl-DEOF (end-of-file)
DELrmdelete file(s)
DELTREErm -rfdelete directory recursively
DIRls -ldirectory listing
ERASErmdelete file(s)
EXITexitexit current process
FCcomm, cmpfile compare
FINDgrepfind strings in files
MDmkdirmake directory
MKDIRmkdirmake directory
MOREmoretext file paging filter
MOVEmvmove
PATH$PATHpath to executables
RENmvrename (move)
RENAMEmvrename (move)
RDrmdirremove directory
RMDIRrmdirremove directory
SORTsortsort file
TIMEdatedisplay system time
TYPEcatoutput file to stdout
XCOPYcp(extended) file copy

 

Virtually all UNIX and shell operators and commands havemany more options and enhancements than their DOS and batch fileequivalents. Many DOS batch files rely on auxiliary utilities,such as ask.com, a crippled counterpart toread.

DOS supports a very limited and incompatible subset offilename wildcard expansion,recognizing only the*and?characters.

Converting a DOS batch file into a shell script is generallystraightforward, and the result ofttimes reads better than theoriginal.


Example L-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.


Example L-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' ShellScripts on the PCsite has a set of comprehensivetutorials on the old-fashioned art of batch fileprogramming. Certain of his ingenious techniques could conceivablyhave relevance for shell scripts.