How do I conditionally redirect the output of a command to /dev/null?

前端 未结 4 1597
不思量自难忘°
不思量自难忘° 2020-12-31 04:51

I have a script. I would like to give this script a quiet mode and a verbose mode.

This is the equivalent of:

if $verbose
then
  redirect=\"> /dev         


        
4条回答
  •  孤独总比滥情好
    2020-12-31 05:37

    Consider whether it would be worth setting set -x for detailed logging to stderr in verbose mode. If that's so, then verbose-only output can be achieved with a no-op : like this.

    while getopts "v" o
    do case "$o" in
       v) set -x;;
       esac
    done
    
    echo "This will always be output"           # goes to stdout
    : this will only be output in verbose mode  # goes to stderr
    

    : evaluates it's arguments but does nothing with them. set -x will show what was evaluated on stderr as each statement is executed.

    It also lets you split verbose and standard logs by stream.

    Might not be what you need here, but it can be a handy trick.

提交回复
热议问题