Results 1 to 7 of 7

Thread: suppress stderr in a bash script

  1. #1
    Join Date
    Sep 2007
    Location
    Vienna
    Beans
    234

    Question suppress stderr in a bash script

    hi,

    i have a bash script that in some cases will print out error messages from various commands like ls, grep, head, tail. currently i see the following options:

    -supplement each single one of those commands with "2>/dev/null" <--doing that currently
    -tell the people who use the script to invoke it with "2>/dev/null"
    -build (otherwise unnecessary) tests for files and directories to prevent errors
    -have confusing error messages printed to the user

    but i was wondering if there was some kind of global setting for that, to the same effect as invoking the script itself with "2>/dev/null", maybe like a silent argument in the "#!/bin/bash" line or something? hope i am making sense...
    As you can see from my history, i was once an enthusiastic ubuntu user. I would hereby like to assert that i no longer recommend using ubuntu to anyone. I recommend using one of the many distributions that do not include ad-/spyware by default.

  2. #2
    Join Date
    Aug 2009
    Beans
    27

    Re: suppress stderr in a bash script

    If you really think, that the errors are going to be confusing to the users, then you shall use tests before calling external commands to prevent errors.

    Otherwise, let the users see the errors.

    Anyway, from what I've observed (in the scripts, that are a part of the system, like the initscripts), the common practice is to test before calling, unless it is desired to show the possible stderr output to the user.

  3. #3
    Join Date
    Feb 2007
    Location
    Southern Germany
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: suppress stderr in a bash script

    i agree that supressing all errors by default is not the best way to go; something like
    Code:
    #!/usr/bin/env bash
    
    func ()
    {
        ls thisfiledoesnotexist
    }
    
    coproc func 2> /dev/null
    should work though.

  4. #4
    Join Date
    Feb 2008
    Location
    Lancashire, UK
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: suppress stderr in a bash script

    An easy kludge to make the redirect of stdout global to the script is to turn the whole script into a conditional that's always true and redirect the conditional block. Something like this:

    Code:
    #!/bin/bash
    
    if [ true ] ; then
    
    # script goes here:
    
    echo hello world
    ls nofile
     
    # end if-block and redirect:
    fi 2>/dev/null

  5. #5
    Join Date
    Feb 2007
    Location
    Southern Germany
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: suppress stderr in a bash script

    Quote Originally Posted by John Bean View Post
    ... redirect the conditional block.
    that's a nice idea! you even don't need a conditional; just a block, i think:

    Code:
    {
        ls strangefile
    } 2> /dev/null

  6. #6
    Join Date
    Feb 2008
    Location
    Lancashire, UK
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: suppress stderr in a bash script

    Quote Originally Posted by buschi View Post
    that's a nice idea! you even don't need a conditional; just a block, i think
    Absolutely; a block is a block is a block...

    I'm so used to using "if [ true/false ]" for execution control while debugging I used it here purely from habit

  7. #7
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: suppress stderr in a bash script

    hi - try this
    #!/bin/bash
    if [ ! $MY_DEBUG ] # if not set -> every time true
    then
    exec 2>/dev/null # true for the whole script
    fi
    ls file_does_not_exist # no output if MY_DEBUG is not set
    ls file_exists # output
    exit 0
    call your script "./my_script" will cause no output on stderr
    call your script "MY_DEBUG=1 ./my_script" will cause error output on /dev/tty
    MY_DEBUG=1 is set in front of your script means that the value is pased to your shell but not exported (i think that's useful).
    if you want your stderr output back on any place insert: exec 2>/dev/tty
    ciao
    Last edited by rnerwein; January 2nd, 2010 at 08:54 PM.

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
  •