Thursday, April 19, 2012

Cross Platform Shell Scripting

How to write a shell script that will run in both  dos and unix environments.

Create a file ending with the extension ".bat"  This is needed on Windows systems.  Unix systems don't require a specific file extension.

#!/bin/sh

goto dossec \
>/dev/null 2>&1

echo Unix version
exit 0

:dossec

echo "Dos version"


The # is a comment in DOS, so it skips this line.  DOS then executes the goto line and jumps to the :dossec line.

Unix errors on the goto line and falls through to the section immediately following the line.  The error is hidden by the redirecting the output of the goto line to /dev/null and redirecting standard error to the same place.

The exit 0 ends the Unix section  and is required to keep the Unix shell from trying to execute Dos commands and generating a lot of errors. 

No comments:

Post a Comment