[NTLUG:Discuss] KSH scripting challenge
Chris Cox
cjcox at acm.org
Thu Sep 17 09:02:06 CDT 2009
Leroy Tennison wrote:
> I want to include some comments in the output of a script but compliance
> also wants the commands contained in the script to be output along with
> their result. The -x switch will accomplish the latter but creates a
> problem with the former.
>
> If I use the echo command in a script (call it demo) as follows:
>
> ksh -x demo
>
> I get
>
> + echo 'Step 1'
> Step 1
>
> If I echo redirect output to /dev/null using the same execution above I get:
>
> + echo 'Step 1'
> + 1> /dev/null
>
> which is better but I still have to live with the unwanted "echo" and "+
> 1> /dev/null" (bash doesn't list the annoying "+ 1> /dev/null" but it's
> not an option).
>
> The -x option doesn't output comments (at least not ones beginning with
> a #).
>
> An example of what I really want the output to look like is -
>
> Step 1
> + grep -v ^# inittab | grep initde
> id:5:initdefault:
> + grep ca: inittab
> ca::ctrlaltdel:/sbin/shutdown -r -t 4 now
>
> and so on.
>
> Any ideas or better suggestions?
Well... I think I lost something in the translation of the above. But here's my attempt at trying to understand what you are wanting:
: Step 1
grep -v '^#' /etc/inittab | grep initde
grep ca: /etc/inittab
Now... the pipeline will likely create a fork and thus each element of the pipeline will show up in the output as an executed statement by itself.
That might now be what you want, but that's pretty much what you get. In your case, the end result could be achieved without the dual grep.
The ':' CAN act like a comment, just that it has evaluation side effects as well. But as long as special evaluated characters are avoided or escaped,
this works... if I get what you're wanting. If this is to be captured to a file make sure to redirect stdout and stderr to the file.
ksh -x demo >demo.out 2>&1
More information about the Discuss
mailing list