in reply to odd escape chars in strings obtained by SSH

As mentioned above, your "ls" is trying to be clever, and you just need to disable that -- but that's not the only source of the escape characters you're getting. The .bashrc (or maybe .profile ?) for the user account is setting the command prompt string to something "fancy" (maybe bold font?) as well.

If you can edit the .bashrc for this user account on the target machine, you can control both the "ls" behavior and the command prompt string:

alias ls='/bin/ls --color=never' PS1='$ '
(or maybe you want the prompt string to be something particular other than '$ ' -- it's up to you). If you can't edit that .bashrc file, just issue those two command lines first thing when you connect, and then only pay attention to the lines that are returned after that point.

if ( wantarray ) { my @a = split "\n", $r; # chomp( @a ); return \@a; } else { return $r; }
(the chomp removes zero chars, by the way)
Well, in the case where "wantarray" returns true, of course chomp does nothing, because split /\n/ removes all the line-feed characters.

Replies are listed 'Best First'.
Re^2: odd escape chars in strings obtained by SSH
by Fletch (Bishop) on Oct 11, 2007 at 18:44 UTC

    And it's a good practice when running something like this (i.e. via something Expect-ish) to "sanitize" the remote environment. Things such as setting your prompt to a (simple) static value and sending the PATH you're expecting to be be in effect makes looking for prompts and debugging so much easier and more dependable. Save the angry fruit salad with 4 different typefaces for interactive use.

Re^2: odd escape chars in strings obtained by SSH
by danmcb (Monk) on Oct 12, 2007 at 08:24 UTC

    Thanks for the suggestions. I have indeed aliased ls and set a simple prompt as suggested. I still do have, however, one escape sequence appended to the end of output (even a simple command like 'pwd'). It looks like this:

    ESC]0;username@hostname:~^G

    ESC is hex 1b, and ^G is the bell char (07). I'm still trying to figure if this is coming from SSH, the term, or from Expect. I'm going to hack it out for now ...

      This looks like it may be the shell prompt. Try setting the shell environment variable PS1='' on the remote machine.

      It should work perfectly the first time! - toma
Re^2: odd escape chars in strings obtained by SSH
by danmcb (Monk) on Oct 12, 2007 at 07:46 UTC
    hah! good point about the chomp. The one on the array is indeed redundant. There is another one there, one the scalr before it gets split, that also removes none.