How do *you* write portable shell scripts?

luke.kendall@cisra.canon.com.au luke.kendall@cisra.canon.com.au
Fri Jul 16 02:01:00 GMT 2004


Thinking about this:

> It would be nice if exim *itself* reported that running exim-config
> might be a good idea.  (Is exim-config used on other platforms besides
> cygwin?)

.... made me wonder what other people on this list do, to write portable
shell scripts?

In a shell script, is there an accepted way to detect you're running
Windows, so you can make necessary adjustments?

One day, I'd like all the hundreds of scripts that we've written locally
and which are still in use, to work as-is under Cygwin.
Attached is the klunky script I use to help with that.  (These days, it
seems I don't need DEVNULL and DEVTTY, so I confess I've stopped using
them ...)

luke
-------------- next part --------------
#!/bin/sh
#
# Set some variables to make DOS-portable shell scripts possible.
# This script must be sourced by a Bourne-like shell to be effective:
#
#	. portshell
#
# Afterwards, you can use these environment variables:
#
#	UNIX_OR_WIN:	"windows" or "unix".
#	UWIN, ISCYGWIN:	Only one of these can be true (else, false).
#	UNIXY:		true if it's quite unix-like (else, false).
#	DEVNULL,DEVTTY:	Use these instead of /dev/tty etc., for portability.
#	EGREP_SILENT:	The option to use to make egrep silent.
#	TMP:		What to use for /tmp.
#
# Author: Luke Kendall
#
# Copyright (C) Luke Kendall, 2001.  Use however you like.
#
PORTSHELL_VARS="DATE_FMT DEVTTY DEVNULL EGREP_SILENT ISCYGWIN \
    UNIX_OR_WIN UNIXY UWIN TMP"
if [ "x$PORTSHELL" != "xstuff" ]
then
    UNIXY=true	# Well, you're running this shell script, aren't you?
    UWIN=false
    ISCYGWIN=false

    # This used to be -f and -s for U/Win, but not needed in v 2.9 and later.
    #
    DATE_FMT="+"
    EGREP_SILENT="q"

    _uname=`uname`
    case "$_uname" in
	UWIN*)
	    UWIN=true
	    DATE_FMT="-f "	# For old U/Win back compatibility.
	    EGREP_SILENT="s"
	    ;;
	CYGWIN*)
	    ISCYGWIN=true
	    UNIXY=true
	    ;;
	*)
	    # If you discover new shells that can't handle dev tty etc. as
	    # below, then tailor it here.
	    ;;
    esac
    if $UNIXY
    then
	DEVTTY=/dev/tty
	DEVNULL=/dev/null
	TMP=${TMP:-/tmp}
	if [ -s c:/boot.ini ]
	then
	    UNIX_OR_WIN="windows"
	else
	    UNIX_OR_WIN="unix"
	fi
    else
	DEVTTY=con
	DEVNULL=nul
	TMP=${TMP:-$TEMP}
	UNIX_OR_WIN="windows"
	if [ "x$TMP" = "x" ]
	then
	    if [ -d c:/tmp ]
	    then
		TMP=c:/tmp
	    elif [ -d c:/temp ]
	    then
		TMP=c:/temp
	    else
		echo "Couldn't work out what to set TMP to" >&2
	    fi
	fi
    fi

    export $PORTSHELL_VARS
    unset _uname
    PORTSHELL="stuff" export PORTSHELL
fi
if [ "x$1" = "x-v" ]
then
    shift
    for v in $PORTSHELL_VARS
    do
	echo "$v=`eval echo '$'$v`"
    done
fi
unset PORTSHELL_VARS
if [ $# != 0 ]
then
    echo "usage: . portshell [-v]" >&2
fi

-------------- next part --------------
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


More information about the Cygwin mailing list