Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix line breaks output by script(1) utility?

I have the following lftp script to copy files from a remote to local:

env TERM=dumb script -a $LOGSTDOUT -c "$(cat <<- EOF
    lftp $PROTOCOL://$URL -u ${USER},${PASS} << EOFF
    set dns:fatal-timeout never
    set sftp:auto-confirm yes
    set mirror:use-pget-n 50
    set mirror:parallel-transfer-count 2
    set mirror:parallel-directories yes
    set mirror:include-regex $REGEX
    set log:enabled/xfer yes
    set log:file/xfer $LOG
    set xfer:use-temp-file yes
    set xfer:temp-file-name *.lftp
    mirror -c -v --loop --Remove-source-dirs "$REMOTEDIR" "$LOCALDIR"
    quit
    EOFF
EOF
)"

I am capturing terminal output with the script(1) utility. The env TERM=dumb is just a random piece of code I found to disable ANSI escape codes.

My problem is that the line breaks of the output log file get quiet mangled. It seems to be using CR and LF. I discovered more information here and it seems this is by design. Though I'm not sure how to fix it.

These line endings cause issues when viewing the logs in lnav:

enter image description here

The reason for this becomes quickly apparent upon inspecting the raw text:

enter image description here

I have thought of some potential options, but not sure how to implement:

  1. Fix the output of the script(1) utility so that single CR are converted to LF. Maybe this can be achieved with piping or some argument?

  2. A hack for lnav to treat CR as LF when displaying in the GUI.

Does anyone know how I can fix these line breaks so it shows correctly in lnav?

like image 813
mwsmws22 Avatar asked Oct 20 '25 06:10

mwsmws22


1 Answers

Try replacing

... script -a $LOGSTDOUT ...

with

... script -a >(tr -d '\r' >"$LOGSTDOUT") ...
  • See the Process Substitution section on the Bash Reference Manual for an explanation of >(...).
  • Note that ALL_UPPERCASE variable names (like LOGSTDOUT) are best avoided because there is a danger of clashes with the large number of special ALL_UPPERCASE variables that are used in shell programming. See Correct Bash and shell script variable capitalization.
  • The quotes on $LOGSTDOUT are necessary in general. Use Shellcheck to find common problems with shell code, including missing quotes.
like image 148
pjh Avatar answered Oct 21 '25 20:10

pjh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!