Вопрос

This is a problem that has been driving me nuts, and I can't figure out a solution. My program:

#!/usr/bin/sh
ssh -t myuser@anotherhost "cat ~/severalLineFile"

and ~/severalLineFile on anotherhost looks like:

line1
line2
line3
line4
line5
line6

When I run my program by itself, the output to my terminal looks as expected. However, when resize my terminal such that it is only 5 rows, and pipe my program to less, it looks like:

line1
     line2
          line3
               line4
                    :

and when pressing space bar at that point, it prints out line5 and line6 (and any additional lines) like:

line5
line6

now i understand that this is a result of running the ssh in a pseudo-terminal, and that this stair-stepping happens because of carriage returns being included in the newline. I've tried using stty ocrnl but this doesn't do what I want, namely for that initial print of less to behave like everything after I press spacebar.

Btw I need to run ssh in -t mode because I'd like all ctrl+C keyboard interrupts to make their way though to the remote process. If there's a solution on this side of things, i'm all ears. I am on Linux Server 6.1, terminal is through Mac OS 10.6.8

I've also tried to replace the \r\n that the pseudo-terminal produces with \n, but this doesn't solve the problem.

Это было полезно?

Решение

To remove extra carriage return characters you have to use stty -onlcr, not stty onlcr (see: Extra Carriage Return on Each Printed Line).

What happens if you first pipe the output of the ssh command to cat -v and then to less?

ssh -t localhost "stty -echo -onlcr; cat testfile.txt; stty echo onlcr" | cat -v | less -U

If you want your ctrl+C keyboard interrupts to propagate through to the remote process, you may try an "EOF to SIGHUP" converter via a named pipe (see: ssh command unexpectedly continues on other system after ssh terminates; to kill the entire remote shell use: kill -HUP -- -$$).

Другие советы

Your problem is EOL markers; MacOS uses CR (\r aka \x0d) characters, Linux uses LF (\n aka \x0a). Due to supporting terminal drawing characters like the CR, ssh is interpreting them as characters that move the cursor around the screen instead of EOL markers. Try this:

ssh -t myuser@anotherhost "sed -e 's,\\r,\\n,' ~/severalLineFile"

OR

ssh -t myuser@anotherhost "tr '\\r' '\\n' < ~/severalLineFile"

If that doesn't work you can use scp to copy the file to the terminal:

scp myuser@anotherhost:severalLineFile /dev/stdout
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top