[comp.unix.questions] Is there a need for Shell script debugger?

ncsrini@ndsuvax.UUCP (srini) (07/20/88)

    I am interested in finding out from those extensive shell programmers
    and others if there is a need for a debugger, like dbx, sdb, adb etc.
    for shell procedures. 
    If you think there is a need, will you please let me know what kind
    of features would you like the debugger to provide. Are there any
    special/additional features that the debugger should provide for
    shell programs? How many debugging features for shell scripts are 
    already available?
    Do you think that checking for syntactic errors should be done
    by the debugger?
    Which is better - debugger in shell script or debugger written in C?

    Please e-mail your replies to me. I am planning to write one if there
    is a need for one. The most confusing thing for me is that shell
    scripts are interpreted. So, I think, I can't really use the design
    philosophy of other debuggers (dbx, sdb, etc.) for this. Can I?
    I am also not sure if I understand the measure of importance for a
    good user-interface for a debugger.

    Thanks in advance for all your help.

    Srini.

hitz@mips.COM (David Hitz) (07/21/88)

In article <1035@ndsuvax.UUCP> ncsrini@ndsuvax.UUCP (srini) writes:
>    I am interested in finding out from those extensive shell programmers
>    and others if there is a need for a debugger, like dbx, sdb, adb etc.
>    for shell procedures. 

There are enough ways of debugging from within the shell itself that I
don't think a seperate debugger is needed.  At any rate, if more
debugging features are needed, I think the right place for it would not
be a seperate debugger, but a mode built into /bin/sh itself.

Here's a small function that I commonly use to help me debug shell scripts.
(This is for SYSV.3 /bin/sh, by the way.):

	debug() {
		set +x
		echo "debug>> \c"
		while read ___debug
		do
			eval "$___debug"
			echo "debug>> \c"
		done
		echo
		set -x
	}

The idea is, if you put a call to "debug" in the middle of some other
shell code, it functions as a breakpoint, letting you type in shell
commands at the "debug>>" prompt to check out the state of the world,
and maybe even change it.  Use ^D to return to execution of the shell
script itself.

Here's a simple script using "debug", and a typescript of it's execution:

----- simple_test --------------------------------------------------------------
	#!/bin/sh -x

	debug() {
		set +x
		echo "debug>> \c"
		while read ___debug
		do
			eval "$___debug"
			echo "debug>> \c"
		done
		echo
		set -x
	}

	main() {
		echo $FOO
		FOO=bar
		echo $FOO

		debug

		echo $FOO
		FOO=foo
		echo $FOO

		debug

		echo $FOO
		echo foo
	}

	main ${1+"$@"}
--------------------------------------------------------------------------------

My use of "main()" has nothing to do with the "debug" function.  That's
just how I write shell scripts.

And here's the typescript from running the simple script:

	Script started on Wed Jul 20 11:09:04 1988
	% ./simple_test
	+ main
	+ echo 

	FOO=bar
	+ echo bar 
	bar
	+ debug 
	+ set +x 
	debug>> echo $FOO
	bar
	debug>> FOO="Changed from bar"
	debug>> ^D
	+ echo Changed from bar 
	Changed from bar
	FOO=foo
	+ echo foo 
	foo
	+ debug 
	+ set +x 
	debug>> echo $FOO
	foo
	debug>> ls
	shsh
	typescript
	debug>> ^D
	+ echo foo 
	foo
	+ echo foo 
	foo
	%  
	script done on Wed Jul 20 11:10:06 1988

Notice that in the first "debug>>" session, I actually changed the
value of $FOO, and that was reflected later in the execution.  In the
second "debug>>" session, I simply looked at $FOO without modifying
it.

This example is quite trival.  I find the "debug" command particularly
useful when I have lots of interacting shell functions in a single
large script.  From the "debug>>" prompt, all the shell functions are
available, so I can test low level ones individually before trying
higher level ones that call them.

If one *were* going to add some kind of debugging to the shell, I would
think that something similar to the above would probably be good.  One
main difference might be that you could set these "break points"
dynamically instead of inserting them in the code as statements.
-- 
Dave Hitz					home: 408-739-7116
UUCP: {decvax,ucbvax,ihnp4}!decwrl!mips!hitz 	play: 408-991-0345

ron@topaz.rutgers.edu (Ron Natalie) (07/22/88)

Frankly I doubt that there is much need for a "shell script" debugger.
Unlike C programs, shell is interpretted and the shell already provides
you some nice features (-x -v flags) to watch it's progress.  The only
debug features I can think of as actually being useful you couldn't
implement as either a script or a C program, but would actually need
to take place as a modification to the Shell itself.  These features
would be things like the ability to single step through the script,
examining or setting variables as neccessary between the steps.

-Ron