Linux

如何防止/保持 tail 將 r 替換為 n

  • April 8, 2019

當我在背景中播放帶有 mpv 的影片時,我得到了這個:

$ tail -f nohup.out 
Playing: https://www.facebook.com/TBN/videos/1580372468665943/
(+) Video --vid=1 (*) (h264 1266x720 30.000fps)
(+) Audio --aid=1 (*) 'DASH audio' (aac 1ch 48000Hz) (external)
File tags:
Artist: TBN
Date: 20180113
Title: Joseph Prince joins Matt & Laurie TONIGHT on PRAISE... watch NOW!
AO: [pulse] 48000Hz mono 1ch float
VO: [gpu] 1266x720 yuv420p
AV: 00:00:00 / 00:56:50 (0%) A-V:  0.000
AV: 00:00:00 / 00:56:50 (0%) A-V:  0.000
AV: 00:00:00 / 00:56:50 (0%) A-V:  0.000
... # THERE ARE QUITE A LOT OF LINES
AV: 00:00:03 / 00:56:50 (0%) A-V:  0.000
AV: 00:00:03 / 00:56:50 (0%) A-V:  0.000
Saving state.


Exiting... (Quit)
^C

但我希望有這個輸出(就像我播放沒有 的文件時一樣nohup):

$ mpv --no-resume-playback https://www.facebook.com/TBN/videos/1580372468665943/
Playing: https://www.facebook.com/TBN/videos/1580372468665943/
(+) Video --vid=1 (*) (h264 1266x720 30.000fps)
(+) Audio --aid=1 (*) 'DASH audio' (aac 1ch 48000Hz) (external)
File tags:
Artist: TBN
Date: 20180113
Title: Joseph Prince joins Matt & Laurie TONIGHT on PRAISE... watch NOW!
AO: [pulse] 48000Hz mono 1ch float
VO: [gpu] 1266x720 yuv420p
AV: 00:00:03 / 00:56:50 (0%) A-V:  0.000
Saving state.


Exiting... (Quit)

EDIT1:我使用 tty like /dev/pts/2,因此如果我鍵入stty inlcr它應該會影響目前的 tty。

我仍然得到相同的混亂輸出tail -f nohup.out

EDIT2:根據@mosvytail的說法,這不是罪魁禍首,而是mpv

我想出了一種方法來對mpv‘s進行 S&R stderr

$ mpv --no-resume-playback https://www.facebook.com/TBN/videos/1580372468665943/ 2>&1 | perl -p -e '$| = 1;s/\n/\r/g if $_ =~ /^AV:/;s/Saving state/\nSaving state/' | tee mpv_all.log
Playing: https://www.facebook.com/TBN/videos/1580372468665943/
(+) Video --vid=1 (*) (h264 634x360 30.000fps)
(+) Audio --aid=1 (*) (aac 1ch 48000Hz)
AO: [pulse] 48000Hz mono 1ch float
VO: [gpu] 634x360 yuv420p
AV: 00:00:08 / 00:56:50 (0%) A-V:  0.000 Cache: 1024s+86MB
Saving state.
[ffmpeg] https: Will reconnect at 63421312 in 0 second(s), error=End of file.


Exiting... (Quit)

編輯 3:@mosvy 謝謝,它現在可以工作了:

$ nohup sh -c ' mpv --no-resume-playback https://www.facebook.com/TBN/videos/1580372468665943/ 2>&1 | perl -p -e '\''$| = 1;s/\n/\r/g if $_ =~ /^AV:/;s/Saving state/\nSaving state/'\'' ' &
$ tail -f nohup.out
Playing: https://www.facebook.com/TBN/videos/1580372468665943/
(+) Video --vid=1 (*) (h264 634x360 30.000fps)
(+) Audio --aid=1 (*) (aac 1ch 48000Hz)
AO: [pulse] 48000Hz mono 1ch float
VO: [gpu] 634x360 yuv420p
AV: 00:00:08 / 00:56:50 (0%) A-V:  0.000 Cache: 1767s+147MB
Saving state.
[ffmpeg] https: Will reconnect at 103693952 in 0 second(s), error=End of file.


Exiting... (Quit)
^C

編輯 4:感謝@jw013 的另一個解決方案:

export perl_script='$| = 1;s/\n/\r/g if $_ =~ /^AV:/;s/Saving state/\nSaving state/'
mpv="command mpv"
args=("$@")
nohup sh -c "$mpv ${args[*]} 2>&1 | perl -p -e '$perl_script' | tee ~/mpv_all.log" &

~~我怎樣才能做到這一點 ?~~解決了 !

tail不替換為。_\r``\n

是您的mpv程序根據其 stderr 是否為終端來調整其狀態輸出。

關於tail不替換\rwith的簡單測試\n

$ nohup sh -c 'while printf "s=$((s=s+1))\r"; do sleep 1; done' &
[1] 3897
$ nohup: ignoring input and appending output to 'nohup.out'

$ tail -f nohup.out
s=8  # s=9, s=10 ... on the same line

就像它的 parent 一樣mplayermpv當有人試圖以與作者想像的不同的方式執行它時,它是相當討厭的;當 stderr 是正常文件或管道時,我無法找到任何選項讓它以與終端相同的方式列印其狀態。您可以做的最簡單的事情是在單獨的screen/tmux視窗中執行它,而不是使用nohup.

引用自:https://unix.stackexchange.com/questions/510888