Anda di halaman 1dari 4

Advanced Bash-Scripting Guide: 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
% / \ = = ! = = ! | @ * > > > < % V A R % R E M N O T N U L E C H O E C H O . E C H OO F F

Shell Script Equivalent Meaning $ / = != | set + v * > >> < $VAR # !


/ d e v / n u l l

command-line parameter prefix command option flag directory path separator (equal-to) string comparison test (not equal-to) string comparison test pipe do not echo current command filename "wild card" file redirection (overwrite) file redirection (append) redirect s t d i n environmental variable comment negate following test "black hole" for burying command output echo (many more option in Bash) echo blank line do not echo command(s) following "for" loop label jump to another location in the script

echo echo set + v none (unnecessary) none (use a function)

F O R% % V A RI N( L I S T )D O for var in [list]; do : L A B E L G O T O

P A U S E C H O I C E I F I FE X I S TF I L E N A M E I F! % N = = ! C A L L C O M M A N D/ C S E T S H I F T S G N E R R O R L E V E L C O N P R N L P T 1 C O M 1

sleep case or select if if [ -e filename ] if [ -z "$N" ]

pause or wait an interval menu choice if-test test if file exists if replaceable parameter "N" not present

source or . (dot operator) "include" another script source or . (dot operator) "include" another script (same as CALL) export shift -lt or -gt $?
s t d i n / d e v / l p 0 / d e v / l p 0 / d e v / t t y S 0

set an environmental variable left shift command-line argument list sign (of integer) exit status "console" (s t d i n ) (generic) printer device first printer device 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
A S S I G N A T T R I B C D C H D I R C L S C O M P C O P Y C t l C C t l Z D E L D E L T R E E D I R E R A S E E X I T F C

ln chmod cd cd clear diff, comm, cmp cp Ctl-C Ctl-D rm rm -rf ls -l rm exit comm, cmp

link file or directory change file permissions change directory change directory clear screen file compare file copy break (signal) EOF (end-of-file) delete file(s) delete directory recursively directory listing delete file(s) exit current process file compare

F I N D M D M K D I R M O R E M O V E P A T H R E N R E N A M E R D R M D I R S O R T T I M E T Y P E X C O P Y

grep mkdir mkdir more mv $PATH mv mv rmdir rmdir sort date cat cp

find strings in files make directory make directory text file paging filter move path to executables rename (move) rename (move) remove directory remove directory sort file display system time output file to s t d o u t (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
R E MV I E W D A T A R E MI N S P I R E DB YA NE X A M P L EI N" D O SP O W E R T O O L S " R E M B YP A U LS O M E R S O N

@ E C H OO F F I F! % 1 = = !G O T OV I E W D A T A R E M I FN OC O M M A N D L I N EA R G . . . F I N D" % 1 "C : \ B O Z O \ B O O K L I S T . T X T G O T OE X I T 0 R E M P R I N TL I N EW I T HS T R I N GM A T C H ,T H E NE X I T . : V I E W D A T A T Y P EC : \ B O Z O \ B O O K L I S T . T X T|M O R E R E M S H O WE N T I R EF I L E ,1P A G EA TAT I M E .

: E X I T 0

The script conversion is somewhat of an improvement. [1] Example N-2. viewdata.sh: Shell Script Conversion of VIEWDATA.BAT
# ! / b i n / b a s h #v i e w d a t a . s h #C o n v e r s i o no fV I E W D A T A . B A Tt os h e l ls c r i p t . D A T A F I L E = / h o m e / b o z o / d a t a f i l e s / b o o k c o l l e c t i o n . d a t a A R G N O = 1 #@ E C H OO F F i f[$ #l t" $ A R G N O "] t h e n l e s s$ D A T A F I L E e l s e g r e p" $ 1 "$ D A T A F I L E f i e x i t0 C o m m a n du n n e c e s s a r yh e r e . #I F! % 1 = = !G O T OV I E W D A T A #T Y P EC : \ M Y D I R \ B O O K L I S T . T X T|M O R E #F I N D" % 1 "C : \ M Y D I R \ B O O K L I S T . T X T

#: E X I T 0

# G O T O s ,l a b e l s ,s m o k e a n d m i r r o r s ,a n df l i m f l a mu n n e c e s s a r y . # T h ec o n v e r t e ds c r i p ti ss h o r t ,s w e e t ,a n dc l e a n , # +w h i c hi sm o r et h a nc a nb es a i df o rt h eo r i g i n a l .

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. Home Next Exercises

Prev Sample . b a s h r cand . b a s h _ p r o f i l eFiles

Anda mungkin juga menyukai