www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2004/07/15/21:34:44

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Date: Fri, 16 Jul 2004 11:34:36 +1000 (EST)
From: luke DOT kendall AT cisra DOT canon DOT com DOT au
Subject: How do *you* write portable shell scripts?
To: cygwin AT cygwin DOT com
MIME-Version: 1.0
Message-Id: <20040716013436.EECD984C87@pessard.research.canon.com.au>

--34253314-846930886-1089941676=:15017
Content-Type: TEXT/plain; CHARSET=US-ASCII

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

--34253314-846930886-1089941676=:15017
Content-Type: TEXT/plain; CHARSET=US-ASCII
Content-Description: portshell
Content-Disposition: attachment ; filename="portshell"

#!/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


--34253314-846930886-1089941676=:15017
Content-Type: text/plain; charset=us-ascii

--
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/
--34253314-846930886-1089941676=:15017--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019