Path: utzoo!attcan!uunet!husc6!uwvax!oddjob!ncar!ames!amdahl!pyramid!prls!mips!hitz From: hitz@mips.COM (David Hitz) Newsgroups: comp.unix.questions Subject: Re: Is there a need for Shell script debugger? Keywords: Debugger, Shell. Message-ID: <2635@quacky.mips.COM> Date: 20 Jul 88 18:46:28 GMT References: <1035@ndsuvax.UUCP> Reply-To: hitz@quacky.UUCP (David Hitz) Organization: MIPS Computer Systems, Sunnyvale, CA Lines: 125 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