5

Previously I ran without sudo and used tee to display on terminal and log to file:

./my_app | tee path/to/log_file

Now I need sudo:

sudo ./my_app | tee path/to/log_file

However, because of tee when I add sudo my output has weird diagonal offsets:

start of line 1 this is a test
        start of line 1 this is a test
                start of line 1 this is a test
                         start of line 1 this is a test

It happens after I ask the user to enter input on the command line (press ENTER).

It never happened without sudo or tee. Is there a way to use both together?

Raffa
  • 32,237

1 Answers1

5

Documentation

This likely is due to incorrect newline translation by your terminal and I guess that you don't see that behavior written into the log file you use but rather you see normal lines instead ... That is because terminals do post output processing before printing the output and that includes among other things translating LF to CRLF i.e. newline to carriage-return in order to bring each consecutive new line of output to the far left side of the terminal screen.

Post output processing should be enabled by default, but you can explicitly enable it with stty opost and you can reproduce what you see if you disable output post processing like so:

$ stty -opost; seq 3; stty opost
1
 2
  3
   $

... or you can reproduce it by only turning off terminal's output LF to CRLF translation onlcr like so:

$ stty -onlcr; seq 3; stty onlcr
1
 2
  3
   $ 

So, you could enable those two explicitly like:

$ stty opost onlcr; seq 3
1
2
3
$ 

... to fix that.

Notice that sometimes other terminal line printing settings might be involved in that and you might want to give combined settings like -nl a try ... To see current settings in your terminal, run stty -a like so:

$ stty -a
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc

... See man stty for more.

Discussion

What is the exact reason for that on your system? ... That can happen for a multitude of reasons and can only be traced on your system which I don't currently have hold of but, you do.

Why with sudo specifically? ... Well sudo gets its own terminal device "pseudo" (mainly to receive input for password … See https://askubuntu.com/a/1449987 ) while it's running your script (especially if it runs in its own shell like when you run your script with sudo without specifying its -b, --background option):

$ sudo tty; tty
/dev/pts/1
/dev/pts/0
$

What does that mean? It means this:

$ { sudo sh -c 'stty -onlcr; seq 3'; seq 4 6; } | tee file
1
 2
  3
   4
5
6
$
$
$ cat file
1
2
3
4
5
6

... notice disabling onlcr under sudo doesn't affect the current terminal's commands output printing but affects those in sudo's ... Also notice that the same output written to the file is not affected as, in reality and in its essence, it's the same:

$ cat -A file
1$
2$
3$
4$
5$
6$

Your use case

It happens after I ask the user to enter input on the command line (press ENTER).

... yes, it does:

$ sudo bash -c 'for i in 1 2 3; do read -t1; echo "$i"; done' | tee
1
 2
  3
   $

... but, starting from the version of sudo used on Ubuntu 23.10, this particularity seems to be fixed (this seems to only affect output when it's sent to a none tty e.g. a pipe ... See source code) ... Special thanks to @DanielT for testing and bringing it to my attention.

Raffa
  • 32,237
  • 1
    The last version your { sudo sh -c 'stty -onlcr; seq 3'; seq 4 6; } | tee file example is reproducible in is 23.04. On my 23.10 and the future 24.04, all numbers are left-aligned. Perhaps this was fixed in the meantime – Daniel T Feb 12 '24 at 01:51
  • @DanielT That is interesting as the providing binary package changed to sudo-ldap on 23.10 and it was sudo on earlier releases ... I will try to look at differences between the two when I have time. – Raffa Feb 12 '24 at 08:17
  • 1
    @DanielT Yep, check the second fix here: https://github.com/sudo-project/sudo/releases/tag/SUDO_1_9_14p2 … That seems what the OP actually exactly has … I will update the answer for that later … Thanks for your testing which lead to the culprit in the specific use case described in OP … Well done Daniel :-) – Raffa Feb 12 '24 at 09:26