Sponsored Content
Full Discussion: How to "tee" stderr
Top Forums Shell Programming and Scripting How to "tee" stderr Post 302515766 by pflynn on Wednesday 20th of April 2011 09:21:58 PM
Old 04-20-2011
If your shell is Bash (or similar), this set of redirections will do the job:

Code:
$ command 3>&1 1>&2 2>&3 | tee file

What does it mean?

The redirection operator n>&m makes file descriptor n to be a copy of file descriptor m. So, whe are:

- Opening a new file descriptor, 3, that is a copy of file descriptor 1, the standard output;
- Making file descriptor 1 a copy of file descriptor 2, the standard error output;
- Making file descriptor 2 to be a copy of file descriptor 3 (the "backup" of the standard output)

in a short: we swapped the standard output and the standard error output.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding custom mesg. when redirecting "exec 2>stderr.err" ?

Doubt regarding using "exec" command to redirect the STDERR to a file. e.g I did it this way. mystage.sh #!/bin/sh exec 2>stage.err .... .... cat stage.err mv: cannot move `/root/stage' to a subdirectory of itself, `/root/stage_old/stage' ls: *.zDB: No such file or... (0 Replies)
Discussion started by: snurani
0 Replies

2. Shell Programming and Scripting

Why stderr file descriptor redirection makes ksh's "select" construct hang.

I am trying to use one global declaration --> "exec 2>$ERR" to capture all stderr outputs that may occur anywhere in my script. Then close it at the end of the script using --> "exec 2<&-" I am using KSH on Solaris 8. KSH Version M-11/16/88i If I comment two "exec .." statements in the... (11 Replies)
Discussion started by: kchinnam
11 Replies

3. Shell Programming and Scripting

tar "--totals" writes to stderr not stdout?

I want to use the "--totals" option in GNU tar for some reporting, however I have discovered that it writes the output to stderr not stdout and I would like to know why. This is running from BASH. mkdir /tmp/test touch /tmp/test/foo.file cd /tmp/ tar --totals -clpzf test.tar.gz test 2>... (2 Replies)
Discussion started by: jelloir
2 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. UNIX for Dummies Questions & Answers

Confused over results using "tee" in piped command

First post here, so hopefully all guidelines are followed, and thanks in advance for any replies. I'm working on a shell script(BASH) that processes a csv file and performs various tasks with the data. All is well, except I want to use 'tee' to send output from 'wc' to a file as well as pipe it... (4 Replies)
Discussion started by: jazzmusic
4 Replies

6. Shell Programming and Scripting

Screen output is blocked by "| tee" command

BACK STORY: I have a script build.py . (It's for creating the ISO file for a special edition of Swift Linux.) This build.py script executes the mintConstructor.py script that I use to modify the Regular Swift Linux ISO to get the special edition Swift Linux ISO. The lines of the script that... (2 Replies)
Discussion started by: swiftlinux
2 Replies

7. Homework & Coursework Questions

C++ with Linux - writing a "tee"-like function

Greetings, everyone. 1. The problem statement, all variables and given/known data: I'm running into a problem with my program concerning the actual output it does. When I open the file that gets the output, it contains a large number of hex(?) variables and not what the user wants. The... (0 Replies)
Discussion started by: assignmentoper
0 Replies

8. Shell Programming and Scripting

The pipe not use "tee" to print on the screen for specific function

I have code fragment like { aa bb cc } > $LOG aa bb cc, all call function "ff", I want "ff" to print on the screen,but others do not print on the scree, is there a method? I can't use "tee", becasue tee I meet the write "error" ff() { echo "hello" } (2 Replies)
Discussion started by: yanglei_fage
2 Replies

9. UNIX for Beginners Questions & Answers

Append content using "tee" command

Hi, How to append content into a file using tee command echo " file1 is archived"| tee -a archive.txt echo " file2 is archived"| tee -a archive.txt echo " file3 is archived"| tee -a archive.txt how to append content as new rows in the archive.txt Thanks, Srinadh. (4 Replies)
Discussion started by: srinadhreddy27
4 Replies

10. Shell Programming and Scripting

Mindboggling difference between using "tee" and "/usr/bin/tee" in bash

I'm on Ubuntu 14.04 and I manually updated my coreutils so that "tee" is now on version 8.27 I was running a script using bash where there is some write to pipe error at some point causing the tee command to exit abruptly while the script continues to run. The newer version of tee seems to prevent... (2 Replies)
Discussion started by: stompadon
2 Replies
TEE(2)							     Linux Programmer's Manual							    TEE(2)

NAME
tee - duplicating pipe content SYNOPSIS
#define _GNU_SOURCE #include <fcntl.h> ssize_t tee(int fd_in, int fd_out, size_t len, unsigned int flags); DESCRIPTION
tee() duplicates up to len bytes of data from the pipe referred to by the file descriptor fd_in to the pipe referred to by the file descriptor fd_out. It does not consume the data that is duplicated from fd_in; therefore, that data can be copied by a subsequent splice(2). flags is a series of modifier flags, which share the name space with splice(2) and vmsplice(2): SPLICE_F_MOVE Currently has no effect for tee(); see splice(2). SPLICE_F_NONBLOCK Do not block on I/O; see splice(2) for further details. SPLICE_F_MORE Currently has no effect for tee(), but may be implemented in the future; see splice(2). SPLICE_F_GIFT Unused for tee(); see vmsplice(2). RETURN VALUE
Upon successful completion, tee() returns the number of bytes that were duplicated between the input and output. A return value of 0 means that there was no data to transfer, and it would not make sense to block, because there are no writers connected to the write end of the pipe referred to by fd_in. On error, tee() returns -1 and errno is set to indicate the error. ERRORS
EINVAL fd_in or fd_out does not refer to a pipe; or fd_in and fd_out refer to the same pipe. ENOMEM Out of memory. VERSIONS
The tee() system call first appeared in Linux 2.6.17. CONFORMING TO
This system call is Linux-specific. NOTES
Conceptually, tee() copies the data between the two pipes. In reality no real data copying takes place though: under the covers, tee() assigns data in the output by merely grabbing a reference to the input. EXAMPLE
The following example implements a basic tee(1) program using the tee() system call. #define _GNU_SOURCE #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <limits.h> int main(int argc, char *argv[]) { int fd; int len, slen; if (argc != 2) { fprintf(stderr, "Usage: %s <file> ", argv[0]); exit(EXIT_FAILURE); } fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd == -1) { perror("open"); exit(EXIT_FAILURE); } do { /* * tee stdin to stdout. */ len = tee(STDIN_FILENO, STDOUT_FILENO, INT_MAX, SPLICE_F_NONBLOCK); if (len < 0) { if (errno == EAGAIN) continue; perror("tee"); exit(EXIT_FAILURE); } else if (len == 0) break; /* * Consume stdin by splicing it to a file. */ while (len > 0) { slen = splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE); if (slen < 0) { perror("splice"); break; } len -= slen; } } while (1); close(fd); exit(EXIT_SUCCESS); } SEE ALSO
splice(2), vmsplice(2), feature_test_macros(7) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2009-09-15 TEE(2)
All times are GMT -4. The time now is 10:24 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy