old-www/LDP/abs/html/dosbatch.html

1600 lines
19 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Converting DOS Batch Files to Shell Scripts</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Sample .bashrc and
.bash_profile Files"
HREF="sample-bashrc.html"><LINK
REL="NEXT"
TITLE="Exercises"
HREF="exercises.html"></HEAD
><BODY
CLASS="APPENDIX"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="sample-bashrc.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="exercises.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="APPENDIX"
><H1
><A
NAME="DOSBATCH"
></A
>Appendix N. Converting DOS Batch Files to Shell Scripts</H1
><P
><A
NAME="DOSBATCH1"
></A
></P
><P
>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.</P
><DIV
CLASS="TABLE"
><A
NAME="AEN24336"
></A
><P
><B
>Table N-1. Batch file keywords / variables / operators, and their shell equivalents</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Batch File Operator</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Shell Script Equivalent</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Meaning</TH
></TR
></THEAD
><TBODY
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>%</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>$</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>command-line parameter prefix</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>/</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>-</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>command option flag</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>\</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>/</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>directory path separator</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>==</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>=</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(equal-to) string comparison test</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>!==!</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>!=</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(not equal-to) string comparison test</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>|</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>|</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>pipe</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>@</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>set <TT
CLASS="OPTION"
>+v</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>do not echo current command</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>*</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>*</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>filename <SPAN
CLASS="QUOTE"
>"wild card"</SPAN
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>&#62;</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>&#62;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>file redirection (overwrite)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>&#62;&#62;</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>&#62;&#62;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>file redirection (append)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>&#60;</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>&#60;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>redirect <TT
CLASS="FILENAME"
>stdin</TT
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>%VAR%</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>$VAR</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>environmental variable</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>REM</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>#</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>comment</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>NOT</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>!</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>negate following test</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>NUL</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="FILENAME"
>/dev/null</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><SPAN
CLASS="QUOTE"
>"black hole"</SPAN
> for burying command output</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>ECHO</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>echo</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>echo (many more option in Bash)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>ECHO.</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>echo</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>echo blank line</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>ECHO OFF</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>set <TT
CLASS="OPTION"
>+v</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>do not echo command(s) following</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>FOR %%VAR IN (LIST) DO</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>for var in [list]; do</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><SPAN
CLASS="QUOTE"
>"for"</SPAN
> loop</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>:LABEL</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>none (unnecessary)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>label</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>GOTO</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>none (use a function)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>jump to another location in the script</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>PAUSE</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>sleep</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>pause or wait an interval</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>CHOICE</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>case or select</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>menu choice</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>IF</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>if</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>if-test</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>IF EXIST <TT
CLASS="REPLACEABLE"
><I
>FILENAME</I
></TT
></TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>if [ -e filename ]</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>test if file exists</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>IF !%N==!</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>if [ -z "$N" ]</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>if replaceable parameter <SPAN
CLASS="QUOTE"
>"N"</SPAN
> not present</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>CALL</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>source or . (dot operator)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><SPAN
CLASS="QUOTE"
>"include"</SPAN
> another script</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>COMMAND /C</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>source or . (dot operator)</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><SPAN
CLASS="QUOTE"
>"include"</SPAN
> another script (same as
CALL)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>SET</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>export</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>set an environmental variable</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>SHIFT</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>shift</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>left shift command-line argument list</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>SGN</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>-lt or -gt</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>sign (of integer)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>ERRORLEVEL</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>$?</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>exit status</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>CON</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="FILENAME"
>stdin</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><SPAN
CLASS="QUOTE"
>"console"</SPAN
> (<TT
CLASS="FILENAME"
>stdin</TT
>)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>PRN</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="FILENAME"
>/dev/lp0</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(generic) printer device</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>LPT1</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="FILENAME"
>/dev/lp0</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>first printer device</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>COM1</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="FILENAME"
>/dev/ttyS0</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>first serial port</TD
></TR
></TBODY
></TABLE
></DIV
><P
><A
NAME="DOSUNIXEQUIV"
></A
></P
><P
>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.</P
><DIV
CLASS="TABLE"
><A
NAME="AEN24545"
></A
><P
><B
>Table N-2. DOS commands and their UNIX equivalents</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
ALIGN="LEFT"
VALIGN="TOP"
>DOS Command</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>UNIX Equivalent</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>Effect</TH
></TR
></THEAD
><TBODY
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>ASSIGN</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>ln</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>link file or directory</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>ATTRIB</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>chmod</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>change file permissions</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>CD</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>cd</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>change directory</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>CHDIR</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>cd</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>change directory</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>CLS</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>clear</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>clear screen</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>COMP</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>diff, comm, cmp</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>file compare</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>COPY</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>cp</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>file copy</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>Ctl-C</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Ctl-C</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>break (signal)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>Ctl-Z</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>Ctl-D</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>EOF (end-of-file)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>DEL</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>rm</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>delete file(s)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>DELTREE</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>rm -rf</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>delete directory recursively</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>DIR</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>ls -l</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>directory listing</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>ERASE</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>rm</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>delete file(s)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>EXIT</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>exit</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>exit current process</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>FC</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>comm, cmp</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>file compare</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>FIND</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>grep</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>find strings in files</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>MD</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>mkdir</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>make directory</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>MKDIR</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>mkdir</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>make directory</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>MORE</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>more</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>text file paging filter</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>MOVE</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>mv</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>move</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>PATH</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>$PATH</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>path to executables</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>REN</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>mv</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>rename (move)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>RENAME</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>mv</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>rename (move)</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>RD</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>rmdir</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>remove directory</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>RMDIR</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>rmdir</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>remove directory</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>SORT</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>sort</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>sort file</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>TIME</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>date</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>display system time</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>TYPE</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>cat</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>output file to <TT
CLASS="FILENAME"
>stdout</TT
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="OPTION"
>XCOPY</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>cp</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>(extended) file copy</TD
></TR
></TBODY
></TABLE
></DIV
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>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 <B
CLASS="COMMAND"
>ask.com</B
>, a crippled counterpart to
<A
HREF="internal.html#READREF"
>read</A
>.</P
><P
>DOS supports only a very limited and incompatible subset of
filename <A
HREF="globbingref.html"
>wild-card expansion</A
>,
recognizing just the <SPAN
CLASS="TOKEN"
>*</SPAN
> and <SPAN
CLASS="TOKEN"
>?</SPAN
>
characters.</P
></TD
></TR
></TABLE
></DIV
><P
>Converting a DOS batch file into a shell script is generally
straightforward, and the result ofttimes reads better than the
original.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="VIEWDAT"
></A
><P
><B
>Example N-1. VIEWDATA.BAT: DOS Batch File</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>REM VIEWDATA
REM INSPIRED BY AN EXAMPLE IN "DOS POWERTOOLS"
REM BY PAUL SOMERSON
@ECHO OFF
IF !%1==! GOTO VIEWDATA
REM IF NO COMMAND-LINE ARG...
FIND "%1" C:\BOZO\BOOKLIST.TXT
GOTO EXIT0
REM PRINT LINE WITH STRING MATCH, THEN EXIT.
:VIEWDATA
TYPE C:\BOZO\BOOKLIST.TXT | MORE
REM SHOW ENTIRE FILE, 1 PAGE AT A TIME.
:EXIT0</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><P
> The script conversion is somewhat of an improvement.
<A
NAME="AEN24713"
HREF="#FTN.AEN24713"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
>
</P
><DIV
CLASS="EXAMPLE"
><A
NAME="VIEWDATA"
></A
><P
><B
>Example N-2. <I
CLASS="FIRSTTERM"
>viewdata.sh</I
>: Shell Script Conversion
of VIEWDATA.BAT</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>#!/bin/bash
# viewdata.sh
# Conversion of VIEWDATA.BAT to shell script.
DATAFILE=/home/bozo/datafiles/book-collection.data
ARGNO=1
# @ECHO OFF Command unnecessary here.
if [ $# -lt "$ARGNO" ] # IF !%1==! GOTO VIEWDATA
then
less $DATAFILE # TYPE C:\MYDIR\BOOKLIST.TXT | MORE
else
grep "$1" $DATAFILE # FIND "%1" C:\MYDIR\BOOKLIST.TXT
fi
exit 0 # :EXIT0
# GOTOs, labels, smoke-and-mirrors, and flimflam unnecessary.
# The converted script is short, sweet, and clean,
#+ which is more than can be said for the original.</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><P
>Ted Davis' <A
HREF="http://www.maem.umr.edu/batch/"
TARGET="_top"
>Shell
Scripts on the PC</A
> site had a set of comprehensive
tutorials on the old-fashioned art of batch file
programming. Unfortunately the page has vanished without a
trace.</P
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN24713"
HREF="dosbatch.html#AEN24713"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>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 <EM
>ABS
Guide</EM
> author, this is wasted effort. A Bash script
can access a DOS filesystem, or even an NTFS partition (with
the help of <A
HREF="http://www.ntfs-3g.org"
TARGET="_top"
>ntfs-3g</A
>)
to do batch or scripted operations.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sample-bashrc.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="exercises.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Sample <TT
CLASS="FILENAME"
>.bashrc</TT
> and
<TT
CLASS="FILENAME"
>.bash_profile</TT
> Files</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Exercises</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>