0

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)
vmrob
  • 2,966
  • 29
  • 40

1 Answers1

0

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$
Community
  • 1
  • 1
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • the `2>&1` was for `"$@"`, but I should probably do that as well. Unfortunately, the problem is still present even with the redirection. I've edited my question appropriately. – vmrob Feb 08 '14 at 10:07
  • Oh, maybe I'm wrong in that case. Will prepare your setup and try to reproduce.. Sounds interesting :) – hek2mgl Feb 08 '14 at 10:09
  • Tried your setup, it gave me an interactive shell without any echo output. I could just leave this with `exit`. Can you explain in one sentence what you are trying to achieve? :) If it's just "suppressing colorized output", there are maybe better ways – hek2mgl Feb 08 '14 at 10:14
  • I'm trying to save colorized output of a command to a variable, test a return status, and then print the output if the return status was non-zero. I'm trying to do this concurrently from make. Sometimes it works fine but most of the time it destroys terminal. – vmrob Feb 08 '14 at 10:16
  • None of those commands, but I plan on using that sort of command with clang from a makefile. – vmrob Feb 08 '14 at 10:26
  • I think I should also mention that I'm using Terminal on a Mac. I'm actually trying it out now on ubuntu and it's not what I expected. – vmrob Feb 08 '14 at 10:32
  • I'm also on Ubuntu. I'll try to find an example for you. I can only say that it will work finally. – hek2mgl Feb 08 '14 at 10:35