Re: [bash] Redirecting both stdout & stderr to the same file

From: Alex Vinokur (alexvn_at_bigfoot.com)
Date: 06/14/03

  • Next message: Michael E. Stora, Ph.D.: "Redirection trouble"
    Date: Sat, 14 Jun 2003 19:08:21 +0300
    
    

    <joe@invalid.address> wrote in message news:m31xxwu442.fsf@invalid.address...
    > "Alex Vinokur" <alexvn@bigfoot.com> writes:
    >
    > > "Ed Morton" <morton@lucent.com> wrote in message news:bcf788$ift@netnews.proxy.lucent.com...
    > > >
    > > > "Alex Vinokur" <alexvn@bigfoot.com> wrote in message
    > > > news:bcf6ph$ipdku$1@ID-79865.news.dfncis.de...
    > > > > How to redirect both stdout & stderr to the same file using bash?
    > > >
    > > > command > file 2>&1
    >
    > > ===========================================
    > > Windows 2000
    > > CYGWIN_NT-5.0 1.3.22(0.78/3/2)
    > > GNU bash, version 2.05b.0(5)-release-(i686-pc-cygwin)
    > > GNU gcc version 3.2 20020927 (prerelease)
    > > ===========================================
    > >
    > > Here is a sample code.
    > >
    > > ------ C code : BEGIN ------
    > > /* File t.c */
    > > #include <stdio.h>
    > > int main()
    > > {
    > > fprintf (stdout, "(1) to stdout\n");
    > > fprintf (stderr, "(2) to stderr\n");
    > > fprintf (stdout, "(3) to stdout\n");
    > > return 0;
    > > }
    > > ------ C code : END --------
    > >
    > > $ gcc t.c
    > >
    > > $ a > zzz 2>&1
    > >
    > > $ cat zzz
    > > (2) to stderr
    > > (1) to stdout
    > > (3) to stdout
    > >
    > >
    > > -----------------------
    > > Expected :
    > > $ cat zzz
    > > (1) to stdout
    > > (2) to stderr
    > > (3) to stdout
    > > -----------------------
    > >
    > > What is wrong?
    >
    > Nothing, stderr is unbuffered, while stdout is fully buffered when
    > output is redirected, so stderr output shows up first.
    >
    > Joe

    Thanks.

    By the way, cout, cerr and clog have no problem with that.

    ===========================================
    Windows 2000
    CYGWIN_NT-5.0 1.3.22(0.78/3/2)
    GNU bash, version 2.05b.0(5)-release-(i686-pc-cygwin)
    GNU gcc/g++ version 3.2 20020927 (prerelease)
    ===========================================

    Here is a sample code.

    ------ C++ code : BEGIN ------
    /* File t.cpp */
    #include <iostream>
    using namespace std;

    int main()
    {
      cout << "(1) to cout" << endl;
      cerr << "(2) to cerr" << endl;
      clog << "(3) to clog" << endl;

      cout << "(4) to cout" << endl;
      cerr << "(5) to cerr" << endl;
      clog << "(6) to clog" << endl;

      return 0;
    }

    ------ C++ code : END --------

    $ g++ t.cpp

    $ a > zzz 2>&1

    $ cat zzz
    (1) to cout
    (2) to cerr
    (3) to clog
    (4) to cout
    (5) to cerr
    (6) to clog

       ==========================================
       Alex Vinokur
         mailto:alexvn@connect.to
         http://www.simtel.net/pub/oth/19088.html
         http://sourceforge.net/users/alexvn
       ==========================================


  • Next message: Michael E. Stora, Ph.D.: "Redirection trouble"

    Relevant Pages

    • Re: passing more than one file.
      ... loop through argv[] until you hit NULL. ... message to stderr and keep going. ... int main(int argc, char *argv) { ...
      (comp.lang.c)
    • Why TCP ECHO Not Work between distant Hosts?
      ... At a Server with IP 163.239.209.16, I typed, ... int main ... WSADATA wsaData; ... fputs(message, stderr); ...
      (microsoft.public.vc.mfc)
    • Help compiling a C utility
      ... int vt_ar5211_eeprom_read(unsigned char *mem, unsigned long int offset, ... usleep(5); /** enable eeprom read access */ ... fprintf(stderr, "eeprom write access failed!\n"); return 1;} ...
      (freebsd-current)
    • Re: convert a string to all-lowercase string
      ... I understand what do you mean but if you can post a sample code please! ... int main{ ... if(isupper((unsigned char) c)) ... or that tolower() can be implemented as a ...
      (comp.lang.c)
    • Re: need a sample code
      ... 1st of all thanks for your sample code. ... >> hai all ... I have separated the sending and the receiving parts ...
      (comp.os.linux.embedded)