Results 1 to 7 of 7

Thread: File Descriptors

  1. #1
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    File Descriptors

    I'm trying to output stdout and stderr to a file, after filtering stderr, but I'm having issues. if I do
    Code:
    clamscan /any/path/* --max-filesize=1 --max-scansize=1 --debug 3>&1 1>&2 2>&3 3>&- | grep 'size exceeded'
    the data is displayed properly, in order. if I try to redirect the output to a file, however, such as
    Code:
    clamscan /path/ -opts redirects>&here | grep 'here' &>out
    then I only receive the filtered data in the file, the data originally in stdout is only printed to the console. if I do one of
    Code:
    clamscan /path/ -opts 2> >(grep 'size exceeded' >> out) >> out
    --- or ---
    clamscan /path/ -opts 1>out 2>&1 | grep 'size exceeded' >> out
    then I receive a file with information out of order, such as
    Code:
    * clamscan output
    * clamscan output
    * clamscan output
    ...
    * debug info
    * debug info
    ...
    at this point I'm stumped.




    Last edited by HiImTye; July 13th, 2013 at 10:36 PM. Reason: tablet ;)

  2. #2
    Join Date
    Jul 2005
    Location
    England
    Beans
    Hidden!

    Re: File Descriptors

    Are you sure you need the ampersand in "&>out" at the end of your command?

    Normally,if you wish to make terminal output into a file, you simply just add "> filename" with no ampersand (the space does not seem to matter, but I always use one for clarity.
    I have no idea if this will make any difference to your problem, and I have never used & in this situation, but it must be worth trying.

    EDIT: Ok I think you can forget this. I've just tried using command &>out and it produces the file as expected, with or without the &.
    Sorry, I should have checked first.
    Last edited by ajgreeny; July 13th, 2013 at 12:46 PM.
    DISTRO: Xubuntu 12.04-64bit --- Code-tags --- Boot-Repair --- Grub2 wiki & Grub2 Basics --- RootSudo

  3. #3
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: File Descriptors

    Code:
    1>file # redirect stdout to file
    >file  # same as above
    2>file # redirect stderr to file
    &>file # redirect stdout and stderr to file



    Last edited by HiImTye; July 13th, 2013 at 08:32 PM.

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: File Descriptors

    Quote Originally Posted by HiImTye View Post
    if I try to redirect the output to a file, however, such as
    Code:
    clamscan /path/ -opts redirects>&here | grep 'here' &>out
    then I only receive the filtered data in the file, the data originally in stdout is only printed to the console.
    Because you redirect only the stdout and stderr of the grep command. You have to create a compound command in order to redirect the output of clamscan as well:
    Code:
    {clamscan /any/path/* --max-filesize=1 --max-scansize=1 --debug 3>&1 1>&2 2>&3 3>&- | grep 'size exceeded';} > file 2>&1
    NOTE: &>word is a bashism. It's useful in interactive bash shells, but in scripts the POSIX form is preferred: >word 2>&1
    Last edited by sisco311; July 14th, 2013 at 12:22 AM.

  5. #5
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: File Descriptors

    I was hopeful that this was going to be the solution, but again it outputs one and then the other instead of them mixed, although at least now it's capturing both stdout and stderr in one go

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: File Descriptors

    Oh, I see. A practical solution would be to grep both the stdout and stderr of the command. It's unlikely that the stdout and stderr of the command are similar.

    I've moved your thread to the Programming Talk forum.

  7. #7
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,661

    Re: File Descriptors

    yeah, I think I'll have to just output them together into a file then filter the file afterwards, or maybe use Sed to filter out anything from the debug that doesn't contain 'size exceeded'

    I ended up just using a series of grep commands
    Last edited by HiImTye; July 14th, 2013 at 04:16 AM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •