• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7929
  • Last Modified:

bash. redirecting error stream

I have a program that outputs text in the normal output stream, and in the error stream.
If I want to capture both outputs, I can do as follows:

./myProgram  2>&1 | tee log.txt

However, in my set-up I cannot use "2>&1".
I need to make a simple script, that could be used as follows:

./myProgram  | log.sh log.txt

"log.sh log.txt" needs to somehow make the equivalent of "2>&1 | tee log.txt".

Can you provide me with such script (log.sh)

0
dportabella
Asked:
dportabella
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
daveWillCommented:
The problem is that | only redirects stdout.  2>&1 ties stderr to stdout.  Unless you can rewrite myProgram, there aren't really any ways around this.
Can you write a myProgram.sh like so:

#!/bin/sh
./myProgram 2>&1

then you could do this:
./myProgram.sh |tee log.txt

is that what you want?
0
 
dportabellaAuthor Commented:
Hello daveWill,

The problem is that I cannot modify "myProgram".
0
 
ozoCommented:
No.
But we can provide you with a script that makes
log.sh ./myProgram  log.txt
the equvalent of
./myProgram  2>&1 | tee log.txt
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
dportabellaAuthor Commented:
Hello Ozo,

I currently have such a script, which also accepts parameters:

log.sh
+++++++++++++++
#!/bin/bash

echo "$2" | sh 2>&1 | tee $1
+++++++++++++++

log.sh ./myProgram param1 param2 param3


But I do not like it.
Why is it not possible to have something like this:
./myProgram  | log.sh log.txt

0
 
ahoffmannCommented:
> Why is it not possible to have something like this:
'cause you only have one STDIN
It only works if your log.sh is the master and redirects STDOUT and STDERR from myProgram as needed.
0
 
Duncan RoeSoftware DeveloperCommented:
Look up bash's exec command (help exec). Observe that is says "If FILE is not specified, the redirections take effect in this shell".
That means you can have a line in your script like:

exec 2>my_error_log

from then on, any stderr output including from any programs run by that script will end up in my_error_log. Is that what you wanted?
0
 
Duncan RoeSoftware DeveloperCommented:
Follow above line with "exec 1>&2" and both stdout & stderr go to my_error_log. But there is no tee - no output to the screen. Is that a problem?
0
 
ahoffmannCommented:
hmm, I assume that some other suggestions are as useful as the graded one ...
0
 
dportabellaAuthor Commented:
ummm,

I see. I didn't understand the first message of daveWill.
Now I do.

Is there a way now to add his answer to the accepted answers also?
otherwise, I can make a new question and invite daveWill to answer it.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now