[comp.unix.shell] "source" exits script, need to trap

daniel@island.COM (Daniel Smith "innovation, not litigation...") (05/16/91)

	I have been writing a large csh script that has many modules/files
that source each other.  I want to find a way to not have the source command
in csh bounce me out of the entire script.  Enclosed is a module that allows
you to push a shell...well sorta.  What you do in the pushed shell is reflected
back in the script (you can change and list variables that the script knows).

	The problem is:  it is really easy to type in something that
forces an error here, this propagates back through all of the nested
sources (typically you might be 3-5 levels into the script before pushing
a shell), and causes the script to exit.  The way that everything starts
out is that the script is run that sets up some initial conditions/aliases
(about 200 lines), and then the module that makes up the main menu is sourced.

	I realize that I can trap a wayward source way up at the top level,
and then dive back into the main menu.  I'm interested in setting a variable
or doing something so that a source that fails gets recognized before
there is a chance for things to backtrack through all of the intervening
modules.  Yup, I've RTFM'd the SunOS 4.1.1 man page for csh(1).  I've been
writing scripts for over 5 years, and have been able to arm wrestle csh
to grok all sorts of things, but this ....!  Without changing the src
for csh concerning the way the source command is handled, is there a way
to trap the source command?  The solution must allow the user to type
in "set" commands that can change variables in the script.

	The following module allows you to type in commands as if
you were in a csh.  A lone RETURN gets you out.  Here's a small wrapper:


#! /bin/csh -f
set tmp_dir=/tmp
again:
	source do_shell
	echo back from do_shell....
	goto again




--- this next part can be sourced from various parts of my script----

#       start of do_shell

set save_dir=$cwd
set history=50
@ cmd_num=1
set nonomatch
unset noglob

#	some attempt to cut down on possible errors...
alias cd 'if (-d \!*) cd \!*'
alias chdir 'echo use cd instead...'

start:
echo -n " $cmd_num ${HOST}:$cwd # "
(dd if=/dev/tty count=1 >! $tmp_dir/foo_sh.$$ ) >& /dev/null
source -h $tmp_dir/foo_sh.$$

#	this catches a lot of errors, but is a costly thing to do...
@ shell_error=`csh -n $tmp_dir/foo_sh.$$ |& wc -c`
if ($shell_error > 0) then
        echo cannot execute that, try again...
else
        source $tmp_dir/foo_sh.$$
endif

# while writing this article, I realized this part should come before...
if (`cat $tmp_dir/foo_sh.$$ | wc -c` == 1) then
        echo all done...
        unalias cd
        unalias chdir
        /bin/rm $tmp_dir/foo_sh.$$
        cd $save_dir
else
        @ cmd_num++
        goto start
endif

ds_2:

#	end of do_shell
-- 
daniel@island.com       Daniel Smith, Island Graphics, (415) 491 0765 x 250(w)
daniel@world.std.com      4000 CivicCenterDrive SanRafael MarinCounty CA 94903
dansmith@well.sf.ca.us      Fax: 491 0402 Disclaimer: Hey, I wrote it, not IG!
falling/yes I'm falling/and she keeps calling/me back again - IJSaF, Beatles

Dan_Jacobson@ATT.COM (05/17/91)

>>>>> On 16 May 91 00:06:32 GMT, daniel@island.COM (Daniel Smith "innovation, not litigation...") said:

DS> 	I have been writing a large csh script that has many
DS> modules/files that source each other.  I want to find a way to not
DS> have the source command in csh bounce me out of the entire script.

Maybe you can intersperse a layer of Bourne shell so you can use "trap".