SUMMARY: how to dynamically add/remove a comment

From: Cohen, Andy (Andy.Cohen@cognex.com)
Date: Fri May 10 2002 - 16:15:45 EDT


I sent this earlier but I don't think it came through. Sorry if this is a
duplicate but I wanted to make sure I got the summary posted.

SUMMARY
=======

As always several speedy and very helpful replies. Basically the manner in
which shell scripts get processed prevent me prevent accomplishing what I
want to using the '# ' as the means to toggle between execution and debug
mode.

Oisin said:
Ignoring control structures such as loops and functions, you are assuming
that
the shell deals with a command script line by line by first making
substitutions,
and then deciding whether the line is a comment line, and then running the
command.
Not so! Rather the conceptual model should be: Remove all explicitly
commented lines
from file, then deal with the result.

So to be able to run in debug mode there were four different ways presented:

1) Alan Davis provide a very thorough elegant 'engineered' approach to this
but, at least for now, would require more setup on my part than I feel this
project warrants.

2) wrap the commands I wish to toggle in an if - fi statement:

  if ($1 != 'debug') then
    compress -v *.dbf
  fi

3) instead of using a # as the means of preventing execution use 'echo'.
This is nice because it not only doesn't execute but it will display the
line so when debugging one can determine, if necessary, whether that line
was reached or not. Though there was a warning:

Note this may not work with compound commands, e.g.

$doit command1 && command2

will execute command2 even in debug mode.

4) a couple of suggestions to use the no-op command ":". This worked
perfectly. Just toggle between nothing and the ':'.

5) there were suggestions to use 'eval' and to make a 'here' document. I
have no experience with either of these techniques.

Thank you everybody!

Andy

ORIGINAL QUESTION
=================
Hi,

I'm writing a korn-shell script and I want to be able to be able to run it
in a 'debug' type mode. That is I want to optionally pass in a parameter
(e.g., 'debug') that will force the script to comment out certain lines that
I don't want to perform. Without this parameter the lines will execute,
with this parameter the lines won't be executed.

The script is setup to be run either with or without the parameter ($1). If
$1 = 'debug' then a variable (SHELL_DEBUG) is set to '# ' (ksh comment). If
$1 is not 'debug' (i.e., absent) then SHELL_DEBUG is set to "" (nothing,
null). For the lines that I want to be able to comment out with this
parameter I put ${SHELL_DEBUG} at the beginning of the line (e.g.,
$SHELL_DEBUG ./cp_hb_archlogs_$ORACLE_SID.sh). This part seems to be
working ok.

The problem is that when run the command line processor doesn't seem to
recognize the "# " as a legitimate comment. If I issue 'set -x' I get:

+ # compress -v *.dbf
./HOT_BACKUPS_734_MULTI_DB.sh[231]: #: not found

so the '#' is getting added to the beginning of the line but then an error
is generated "#: not found".

Am I doing something wrong? Is this even possible? Am I on the right path
or is there some other approach I should take (i.e., 'case' or 'if'
branching)?

Thanks much!
Andy

===================================================
Andy Cohen
Database Systems Administrator
COGNEX Corporation
(508) 650-3079 Fax: (508) 650-3337
andy.cohen@cognex.com www.cognex.com
cell: (617) 470-0034
===================================================



This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 10:48:40 EDT