Re: BUG?: (or what's going on?) test for NL == NL fails (bash-4.3.39(3)-release)

2015-08-02 Thread Chris F.A. Johnson

On Sun, 2 Aug 2015, Linda Walsh wrote:



also noticed this similar strange behavior:

used 'ord'** for this test (trying to compare decimal val of chars)

isatty () { test -c /proc/self/fd/1 ; }
ord () { local nl=; isatty  nl=\n; printf %d$nl '$1 ; } ord () { local nl=; isatty  nl=\n; printf %d$nl '$1 ; } 

ord $(printf \n)


   Trailing newline are stripped from command substitution, making $(printf 
\n) and empty string.

   If you want a newline, use:

ord $'\n'

--
Chris F.A. Johnson, http://cfajohnson.com



Re: BUG?: (or what's going on?) test for NL == NL fails (bash-4.3.39(3)-release)

2015-08-02 Thread Linda Walsh



Charles Daffern wrote:

On 02/08/15 20:30, Linda Walsh wrote:

 if [[ $(printf \n) == $'\n' ]]; then echo T; else echo F; fi

F


The command substitution operators ($(...) and `...`) strip all trailing
linefeeds. You are left with an empty string here.

The usual workarounds I see (where necessary) are hacks like this:

variable=$(command; echo x); variable=${variable%?x}


I see what you mean --- erk...
I think the process substitution doesn't, since you need
to explicitly use a -t switch if you want to strip the \n's off

Thanks!





Re: BUG?: (or what's going on?) test for NL == NL fails (bash-4.3.39(3)-release)

2015-08-02 Thread Linda Walsh


also noticed this similar strange behavior:

used 'ord'** for this test (trying to compare decimal val of chars)

isatty () { test -c /proc/self/fd/1 ; }
ord () { local nl=; isatty  nl=\n; printf %d$nl '$1 ; } 


ord $(printf \n)

0
ord $(printf \t)
9

ord $(printf  )

32

ord $(printf \r)

13
-- but this DOES work:

ord $'\n'

10

Traditional separators and CR work, -- just \n.
Am just looking for an efficient way to test for 0x0a
as last char in a var, ... after another 20 minutes of
trying things at random, found this:

  printf %d\n '${a:0-1:1}'
 10

Oddly, the final single quote in the string seems unnecessary.
But that (or into a var: printf -v var) both work...but I
feel a bit contorted trying so many odd ways to get something
that I thought would be simple, to work.

Still... why so many problems with \n?
I tried unsetting IFS  even setting both eol chars,
explicitly (I wondered if both eol chars were 'undef'
w/stty:


stty

speed 38400 baud; line = 0;
kill = ^X; eol = #; eol2 = #; susp = ^Y;   ## yes, I set both eol chars to '#'
only to find it didn't change anything (LF was still end of line).
:-(






Re: BUG?: (or what's going on?) test for NL == NL fails (bash-4.3.39(3)-release)

2015-08-02 Thread Charles Daffern
On 02/08/15 21:30, Linda Walsh wrote:
 Am just looking for an efficient way to test for 0x0a
 as last char in a var, ... after another 20 minutes of
 trying things at random, found this:

   printf %d\n '${a:0-1:1}'
  10

 Oddly, the final single quote in the string seems unnecessary.
 But that (or into a var: printf -v var) both work...but I
 feel a bit contorted trying so many odd ways to get something
 that I thought would be simple, to work.

Most of the problems here are due to the use of command substitution (as
I mentioned in an earlier response).
The ways I would go about checking for a newline at the end of a string are:

[ $var != ${var%$'\n'} ]
or
[[ $var = *$'\n' ]]





signature.asc
Description: OpenPGP digital signature


BUG?: (or what's going on?) test for NL == NL fails (bash-4.3.39(3)-release)

2015-08-02 Thread Linda Walsh


 printf \n |hexdump -C

  0a

 printf %s $'\n'|hexdump -C

  0a

 if [[ $(printf \n) == $'\n' ]]; then echo T; else echo F; fi

F


just noticed the problem doesn't happen if I try to use '\t' (tab)

 printf \t |hexdump -C

  09

 printf %s $'\t'|hexdump -C

  09

 if [[ $(printf \t) == $'\t' ]]; then echo T; else echo F; fi

T






Re: BUG?: (or what's going on?) test for NL == NL fails (bash-4.3.39(3)-release)

2015-08-02 Thread Charles Daffern
On 02/08/15 20:30, Linda Walsh wrote:
  if [[ $(printf \n) == $'\n' ]]; then echo T; else echo F; fi
 F

The command substitution operators ($(...) and `...`) strip all trailing
linefeeds. You are left with an empty string here.

The usual workarounds I see (where necessary) are hacks like this:

variable=$(command; echo x); variable=${variable%?x}



signature.asc
Description: OpenPGP digital signature