"nmkdir" in the Bourne shell at a Unix machine near you.

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Wed Sep 4 04:42:32 AEST 1985


> >	if cd $d
> > ...
> I think in the bourne shell a bad cd will terminate the shell script
> no matter what its return code.  Therefore, as soon as a bad cd occurs,
> termination before any test.

There is a simple way around this; use a subshell as in (cd $d).
By way of illustration, here the the .funcs file that I source
in my .profile.  (Warning: the suspend() function is for the BRL SVR2
job-control Bourne shell and will not work with the AT&T SVR2 shell.)

# shell functions

DIRSTACK=...	# for pushd, popd

ch(){
	if [ $# -lt 1 ]
	then	cd
	else	cd $1
	fi
	PS1=`uname`:`pwd`' $ '
}

j(){ (set +u; exec jove $*); }

l(){ (set +u; exec ls -bCF $*); }

not(){	$*
	if [ $? -eq 0 ]
	then	return 1
	else	return 0
	fi
}

popd(){
	set $DIRSTACK
	if [ $# -ge 2 ]
	then	DIRSTACK="$*"
		ch $1
		set $DIRSTACK	# ch clobbered $*
		shift
		DIRSTACK="$*"
	fi
}

print(){ (set +u; pr $* | lpr); }

pushd(){
	DIRSTACK=`pwd`" $DIRSTACK"
	if [ $# -lt 1 ]
	then	set $HOME
	fi
	if (cd $1)
	then	cd $1 >&-
		echo $DIRSTACK
		PS1=`uname`:`pwd`' $ '
	else	popd
	fi
}

if not pdp11
then
suspend(){
	case "$-" in
	*J*)	set +j
		kill -18 $$
		set -J
		;;
	*)	kill -18 $$	# SIGSTOP
		;;
	esac
}

readonly suspend
fi

swapd(){
	DIRSTACK=`pwd`" $DIRSTACK"
	set $DIRSTACK
	if [ $# -ge 3 ]
	then	DIRSTACK="$*"
		ch $2
		set $DIRSTACK	# ch clobbered $*
		DIRSTACK="$1"
		shift 2
		DIRSTACK=$DIRSTACK" $*"
		echo $DIRSTACK
	else	shift
		DIRSTACK="$*"
		echo 'swapd: No previous directory' >&2
		return 1
	fi
}

readonly ch j l popd print pushd swapd



More information about the Comp.sources.unix mailing list