简体   繁体   中英

Garbled shell newlines after concurrent script execution

I'm attempting to execute a shell script concurrently from a makefile and have all of the output go to stdout though when I do, the newlines become garbled and the only reliable fix I've found is to reset .

makefile:

all: t1 t2 t3 t4
t1 t2 t3 t4:
    @./test.sh true

test.sh:

#!/bin/bash
script -q /dev/null "$@" 2>&1 > /dev/null 2>&1

Invocation:

$ make -j

No output as you would expect, but sometimes it breaks the terminal and I have to reset .

Sometimes newlines and carriage returns don't work after it runs:

Devbox:Desktop user$ time make -j
real    0m0.019s
                user    0m0.019s
                                sys 0m0.022s
                                                Devbox:Desktop user$

Removing script from the script and replacing that line with "$@" allows it to work just fine, but I'm using script to preserve color output from the command.

LOG=$(script -q /dev/null "$@" 2>&1 | tr -d '\r' | cat)

I think script isn't the proper program here. I once gave an answer how to trick programs to do colorized output, even when their output goes not to a terminal using LD_PRELOAD and an overwritten version of the glibc function isatty() .

Based on that, your test.sh could look like this:

#!/bin/bash
output=$(LD_PRELOAD=./libisatty.so $@)
return_value=$?

echo -e "$output"
exit $return_value

And your Makefile like this:

all: t1 t2 t3 t4$ 
t1 t2 t3 t4:$
^I@./test.sh ls -al --color=auto$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM