Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Unexpected Results/Heisenbug



Attached is a script which surprised me when I tested it.  When run with
DEBUG=0, the output is as I would expect.  When run with DEBUG=1, the
for loop sets ${each} to "continue:" after the debugging code executes. 
Is this a problem with my nubile scripting skills or does this signify a
problem with zsh?  Please cc responses to ken at smith.net as I am not a
subscriber of the zsh-workers mailing list.

Thank you for any tips.
#!/bin/zsh

DEBUG=1

function debugging
{
	[[ ${+DEBUG} == 1 && ${DEBUG} == 1 ]]
}
function format_output
{
	function arg_len
	{
		echo $1 | wc -c
	}

	heading_separator=": "
	prefix=${1}${heading_separator}
	prefix_len=`arg_len ${prefix}`
	if [[ ${+COLUMNS} == 1 ]]; then
	columns=${COLUMNS}
	else
		columns=80
	fi

	terminal_position=0
	first_argument=1
	just_wrote_prefix=0
	for each in $*; do
		if [[ ${first_argument} == 1 ]]; then
			first_argument=0
			continue
		fi
		if [[ ${terminal_position} == 0 ]]; then
			echo -n ${prefix}
			just_wrote_prefix=1
		fi
		token_len=`arg_len ${each}`
		((terminal_position += token_len + 1))
		if [[ ${terminal_position} > ${columns} ]]; then
			# If we just wrote the prefix, then we
			# have to write this token where it stands
			# even though it will wrap.
			if [[ ${just_wrote_prefix} == 1 ]]; then
				echo ${each}
				just_wrote_prefix=0
				((terminal_position = 0))
			else
				echo -n "\n${prefix}${each} "
				((terminal_position =
					prefix_len + token_len))
				just_wrote_prefix=0
			fi
		else
			echo -n "${each} "
			just_wrote_prefix=0
		fi
	done
	echo
}

if debugging; then
	format_output DEBUG Executing \"${0} ${*}\"
fi

for each in test1 test2 test3; do
	echo "***each=${each}"
	if debugging; then
		format_output DEBUG press enter to continue:
		read
	fi
	echo "***each=${each}"
done


Messages sorted by: Reverse Date, Date, Thread, Author