Porting unix shell script files to Windows Batch shell scripts.
Even if the windows command shell is not as powerful as Linux shells, it is
still easy to port (not too complicated) linux scripts under windows.
Below will be explained the main correspondences between, redirection of
output/input and between the main linux tools. There will be more explanation
about windows assuming people are already familiar to linux.
-
Comments:
In a linux scripts, they are made by starting the line with '#'
character, the only change in windows is using '::' (two colon
characters) instead.
-
Environment variables:
When you want to set a environment variable under linux or windows you do more
or less the same.
set A=value for windows or setenv A value for linux (depending on the
shell you are using)
For referencing the syntax is different. Under linux it is referenced as $A
but under windows it is referenced as %A%.
-
Control Loop:
There are only two loop control operator that are used under windows and to
which it is possible to give a comparison.
Under windows you have:
Operator |
Syntax |
Example |
IF |
IF Cond (Ops) |
IF NOT EXIST test.file (echo file missing
&& GOTO :EOF) |
FOR |
FOR %%V in Set DO (Ops) |
FOR %%I in (a.exe b.exe) DO
%%I |
Where Cond is any Boolean condition which can be the result of another command.
In the For loop structure, the variable V can only be one character! Set is
usually a set of Files but it can use a range of value by making the
appropriate syntax (type HELP FOR in the windows terminal to have the
explanation pages). In both case Ops is a sequence of command or a unique
command, the parenthesis enclose the sequence that will be performed if the
condition is true.
Under unix shell it is:
Operator |
Syntax |
Example |
IF |
if Cond; then Ops else Ops fi
|
If !test -f test.file echo file
missing exit
|
FOR |
For $var in Set; do Ops done
|
For $exe in a.exe b.exe; do $exe
|
-
Tools Altering Files:
Everybody knows the rm, cp, mv operations that are surely
used every day. Fortunately under windows their use is also straightforward.
cp becomes copy, rm becomes del and mv becomes
move. Their syntax is the same except for flags.
move only has one flag /Y which corresponds to -f of linux.
del has five flags /P /F /S /Q /A (+ attributes) . The first one
simply correspond to -i flag of rm. /Q corresponds more or
less to -f for rm and the others are not worth explaining here as
they do not exist under linux.
copy has many flags but the only relevant is /Y corresponding to -f
of cp command.
-
Others tools:
-
grep becomes FIND, syntax is the same except that for FIND
the pattern must be enclosed in " ".
-
diff becomes FC
with same syntax
-
sort stays the same, with same syntax
Be aware anyway that not all flags available for linux are available under
windows. Most of the time, you need to simply change -flag into /flag. To be
sure about what flag are available always refer to "HELP command"
which are effectively the equivalent to linux man pages.
-
Redirections:
This point is the easiest and the most troublesome at the same time. It is easy
because all redirections of input and batching are the same (&, <, >,
2>, 1>&2, .).
But it is troublesome because only the << operator does not
exists. Therefore each time you use a pattern like
Command "params" << keyword
Input line 1
Input line 2
.
Keyword
you will have to put the input lines into one file and then use the <
operator to point to that file and have
Command "params" < inputfile.dat
Most of the time I use ".dat" extension to remind me that it is only
data for the batch scripts files.
This was the simple case, the more complex case arise when in the input lines
of the linux version there are lines using environment variables. Obviously
under linux it is substituted at execution time as the input is indirected but
under windows if you simply copy it into a file, the programs in the script
will read it as pure string without substiuting it with the actual value of the
environment variable.
The way I do it (probably not the best way I guess) is just to do the following
echo Input line 1 > inputfile.dat
echo Input line 2 >> inputfile.dat
...
echo Input line last >> inputfile.dat
The > will erase the content if this file previously existed and then writes
the line; the >> will append the text to an existing file. The reason for
using this method is simply because echo will substitute the value of %A% when
printing into a file.