Search code examples
linuxbashloggingsftpnewline

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?


Solution

  • Try replacing

    ... script -a $LOGSTDOUT ...
    

    with

    ... script -a >(tr -d '\r' >"$LOGSTDOUT") ...