Converting a DOS batch file to a shell script

Source: Internet
Author: User
Tags clear screen file copy goto stdin

Converting a DOS batch file to a shell script

Quite a few programmers who learn scripts on a PC are running DOS. In fact, the Disabled DOS batch file language can also write some powerful scripts, although they generally need to rely on external tools. So, at some point, we still need to convert old DOS batch files into Unix shell scripts. In general, it is not difficult to do this because the DOS batch file operation is simply a restricted subset of the equivalent shell script.

table L-1. batch file keywords/variables/operators, and equivalent shell symbols

Batch file Operators shell script equivalence symbol meaning
% $ Command line parameter prefix
/ - Command option Tag
\ / Directory path delimiter
== = (equals) string comparison test
!==! != (unequal) string comparison test
| | Pipeline
@ Set +v Do not print the current command
* * File name "wildcard character"
> > File Redirection (Overwrite)
>> >> File Redirection (additional)
< < REDIRECT StdIn
%var% $VAR Environment variables
Rem # Comments
Not ! Take counter
NUL /dev/null "Black Hole" to block command output
ECHO Echo Print (More options in bash)
ECHO. Echo Print Blank Lines
ECHO OFF Set +v Do not print subsequent commands
For%%var in (LIST) does for var in [list]; Do "for" loop
: LABEL No equivalents (superfluous) Label
Goto No equivalents (using functions) Jump to another location in the script
PAUSE Sleep Pause or wait for some time
CHOICE Case or select Menu selection
IF If If condition statement
IF EXIST FILENAME If [-e filename] Test whether the file exists
IF!%n==! If [-Z "$N"] Whether the parameter "N" exists
Pager The source command or. (dot operator) "include" another script
Command/c The source command or. (dot operator) "include" another script (same as Call)
SET Export Setting an environment variable
SHIFT Shift Move left command-line argument list
SGN -lt or-GT (shaping) symbols
ERRORLEVEL $? Exit status
CON Stdin "Console" (stdin)
PRN /dev/lp0 (general) Printing equipment
LPT1 /dev/lp0 First printing device
COM1 /dev/ttys0 First serial port

Batch files generally contain DOS commands. We have to convert it to UNIX's equivalent command so we can convert the batch file to a shell script file.

table L-2. DOS command and UNIX equivalent command

DOS command UNIX equivalent command effect
ASSIGN ln Link file or directory
ATTRIB chmod Modify file Permissions
Cd Cd Change Directory
CHDIR Cd Change Directory
Cls Clear Clear Screen
COMP diff, Comm, CMP File comparison
COPY Cp File copy
Ctl-c Ctl-c Interrupt (signal)
Ctl-z Ctl-d EOF (end of file)
Del Rm deleting files
deltree Rm-rf Recursively delete a directory
DIR Ls-l Listing Directory Contents
ERASE Rm deleting files
EXIT Exit Exiting the current process
Fc Comm, CMP File comparison
FIND Grep Find a string in a file
Md Mkdir New Catalog
Mkdir Mkdir New Catalog
More More displaying text files on a paginated page
MOVE Mv Moving files
PATH $PATH Path to the executable file
REN Mv Rename (move)
RENAME Mv Rename (move)
RD RmDir Delete Directory
RMDIR RmDir Delete Directory
SORT Sort Sort files
Time Date Show System time
TYPE Cat Output files to stdout
Xcopy Cp (extended) file copy

In fact, almost all UNIX and shell operators, as well as commands, have many options, and they are much more powerful than DOS and batch files. Many DOS batch files need to rely on assistive tools, such as Ask.com, which is a similar copy of a lot worse than the read command.

Dos for File name wildcard extension support is very limited, and is not complete, just identify * and ?.

Converting a DOS batch file to a SEHLL script is usually a simple matter, and the result of the conversion is usually better than the original batch file.

example L-1. VIEWDATA. Bat:dos Batch processing files

1 rem VIEWDATA  2   3 rem inspiration comes from the example "DOS POWERTOOLS"  4 REM                           PAUL Somerson write  5   6   7 @ECHO OFF  8   9 IF!%1==! GOTO VIEWDATA REM  If there are no command line arguments ... FIND "%1" C:\BOZO\BOOKLIST. TXT EXIT0  The REM prints out a string that matches the line and then exits.  15:viewdata-C:\BOZO\BOOKLIST TYPE. TXT | More The REM  displays the entire file, one page at a time.  19:exit0

Some improvements have been made to the conversion script.

example L-2. viewdata.sh: Shell script converted from Viewdata.bat

1 #!/bin/bash  2 # viewdata.sh  3 # Convert shell script from Viewdata.bat.   4   5 datafile=/home/bozo/datafiles/book-collection.data  6 argno=1  7   8 # @ECHO off                 This command is not needed here.   9  If [$#-lt "$ARGNO"]    # if!%1==! GOTO VIEWDATA and then   $DATAFILE          # TYPE C:\MYDIR\BOOKLIST. TXT | More than (else)   grep "$" $DATAFILE     # FIND "%1" C:\MYDIR\BOOKLIST. TXT-Fi   +  exit 0                    #: EXIT0  #  jumps, tags, and other small tricks that you don't need in a shell script.  we can say that the converted script is much better than the original batch file,  21 #+ It is shorter, looks neater and more elegant.

Ted Davis ' shell Scripts on the PC site has many tutorials on old-fashioned batch file programming, and some of the ingenious techniques he uses are similar to shell scripts.

Convert a DOS batch file to a shell script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.