lschuermann
lschuermann

Reputation: 932

Shell-Script Logging

i want to implement a shell-script that runs the command xyz and stores its output in a variable, but at the same time forwarding the commands output to the shell-scripts stdout.

This is because I want to launch this script via launchd, let it automatically log the script's output, but then also let the script push the individual commands output to the web. The script should not simply buffer the commands output and print it after it ran, but rather in real time.

Is something like this possible, and if, how do you implement it?

Thanks

thel30n

Upvotes: 1

Views: 1733

Answers (2)

DZDomi
DZDomi

Reputation: 1679

you are looking for the command:

$VAR=$(echo 'test' | tee /dev/tty)
test

echo $VAR
test

Upvotes: 3

Oleg Andriyanov
Oleg Andriyanov

Reputation: 5289

I believe there is no way to save log into a shell variable and avoid buffering command's output at the same time. But the alternative way would be saving log messages to a file using tee(1). For example:

LOGFILE=/path/to/logfile
run_and_log() {
    $@ | tee -a "$LOGFILE"
}

run_and_log xyz

Upvotes: 1

Related Questions