I'm making a script that executes some commands inside, and these commands show some output on STDOUT
(and STDERR
as well, but that's no problem). I need that my script generates a .tar.gz file to STDOUT
, so the output of some commands executed in the script also go to STDOUT
and this ends with a not valid .tar.gz file in the output.
So, in short, it's possible to output the first commands to the screen (as I still want to see the output) but not via STDOUT
? Also I would like to keep the STDERR
untouched so only error messages appear there.
A simple example of what I mean. This would be my script:
#!/bin/bash
# the output of these commands shouldn't go to STDOUT, but still appear on screen
some_cmd foo bar
other_cmd baz
#the following command creates a tar.gz of the "whatever" folder,
#and outputs the result to STDOUT
tar zc whatever/
I've tried messing with exec
and the file descriptors, but I still can't get it to work:
#!/bin/bash
# save STDOUT to #3
exec 3>&1
# the output of these commands should go to #3 and screen, but not STDOUT
some_cmd foo bar
other_cmd baz
# restore STDOUT
exec 1>&3
# the output of this command should be the only one that goes to STDOUT
tar zc whatever/
I guess I'm lacking closing STDOUT
after the first exec and reopen it again or something, but I can't find the right way to do it (right now the result is the same as if I didn't add the exec
s
stdout is the screen. There isn't a separation between stdout and "the screen".
In this instance, I would just redirect stdout to stderr temporarily with
1>&2
within a subshell. This will cause the commands' output to be shown on screen but won't be in the programs stdout stream.Is there a reason you need to pipe the output of this script into something else? Typically you'd just have tar writing to a file using the
-f
flag or perform a redirect on the tar command only:tar zc whatever > filename.tar.gz
(unless you were putting it onto a device such as a tape or using it as a form of copy).Wouldn't it be much easier if you use the
-f
switch to the tar command to tell tar to write to a file ?If that doesn't do what you want then you will have to redirect individually each command that may write to STDOUT
I think you are looking for some kind of multiplexing.
This is simple example how to append timestamp to each std out line: http://www.podciborski.co.uk/programming/perl/add-a-timestamp-to-every-stdout-line/
You can use some special tag instead of timestamp to mark log lines. Than you would have to remove those lines on the other end. So usage would be like this:
create_tar.sh should be script that prints lines with log tag and redirects another lines to file.