Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stair-stepping when using a pty (through ssh) and piping to more

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.

like image 623
gnr Avatar asked Dec 01 '25 21:12

gnr


1 Answers

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 -- -$$).

like image 102
zanco Avatar answered Dec 03 '25 13:12

zanco