[NCLUG] bash question

Warren Turkal wt at penguintechs.org
Fri Apr 27 19:00:07 MDT 2007


On Fri, Apr 27, 2007 at 04:14:50PM -0700, Marcio Luis Teixeira wrote:
> 
> This works:
> 
>    echo -ne "\n" | od -t x1 -t a
> 
> I think the problem with your example is the outer echo discards the 0x0a -- perhaps it strips off control charaters?
> 
> Notice, that your counter-example, when written using nested echos, also does not work:
> 
> echo -n "$(echo -n '
> ')" | od -t x1 -t a
> 
> 0000000
> 
> So it's the outer echo that's getting rid of the 0x0a.

Is is the outer echo, or is it bash getting rid of it? Is it proper
behavior?

Here's a more fleshed out script and example:

<script>
#!/bin/bash

input_str=$1

function escape_special_chars ()
{
	str=$1
	newstr=$1

	num_escaped=0
	for (( i=0; i<${#str}; i++ )); do
		current_char=${str:$i:1}

		case "$current_char" in
			"\""| \
			"\$"| \
			"\\")
				newstr="${newstr:0:$(( $i + $num_escaped ))}\\${str:$i:$(( ${#str}-$i ))}"
				num_escaped=$(( $num_escaped + 1 ))
			;;
			*)
				#no escape needed
			;;
		esac
	done

	echo -nE "$newstr"
}

echo "escape_special_chars:"
escape_special_chars "$input_str" | od -t x1 -t a

echo "escape_special_chars echoed:"
echo -nE $(escape_special_chars "$input_str") | od -t x1 -t a #bash BUG?

#echo "input_str:"
#echo -nE "$input_str" | od -t x1 -t a
#echo "escaped_str:"
#echo -nE "$escaped_str" | od -t x1 -t a

</script>

<commandline>
wt at pyrus:~/test/ctest$ ./test.sh "aaaa
> "
escape_special_chars:
0000000 61 61 61 61 0a
          a   a   a   a  nl
0000005
escape_special_chars echoed:
0000000 61 61 61 61
          a   a   a   a
0000004
wt at pyrus:~/test/ctest$ ./test.sh "aaaa
a"
escape_special_chars:
0000000 61 61 61 61 0a 61
          a   a   a   a  nl   a
0000006
escape_special_chars echoed:
0000000 61 61 61 61 20 61
          a   a   a   a  sp   a
0000006

</commandline>

This feels like a bug. It seems like bash is screwing my output from
subcommands. BTW, this is more of an intellectual exercise at this point.
I have already written a script to do what I want in Python.

So, can anyone see anything wrong in the above script?

wt
-- 
Warren Turkal
Penguin Techs...Taking the "hertz" out of gigahertz!(TM)
901-338-1337



More information about the NCLUG mailing list