Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to be able to redirect some output of my script to /dev/null based on a command line switch. I don't know how should I do it.

In a silly way, it would be something like this (in a too simplified way):

#!/bin/sh

REDIRECT=

if [ $# -ge 1 -a "$1" = "--verbose" ]; then
    echo    "Verbose mode."
    REDIRECT='1>&2 > /dev/null'
fi

echo "Things I want to see regardless of my verbose switch."

#... Other things...

# This command and others along the script should only be seen if I am in verbose mode.
ls -l $REDIRECT

Any clues, please?

Thanks people.

share|improve this question

closed as off topic by Michael Hampton, HopelessN00b, Ladadadada, Jeff Ferland, Iain Oct 8 '12 at 21:00

Questions on Server Fault are expected to relate to professional server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Looks like stackoverflow.com/questions/8756535/… has the answer you want, more-or-less –  DerfK Aug 6 '12 at 20:46
    
You are right. This link gives me an answer. Sorry for not being able to find it before. Really thanks. –  fljx Aug 7 '12 at 11:34

3 Answers 3

up vote 2 down vote accepted

Tie STDOUT to another handle if you are in verbose mode, otherwise link those handles to /dev/null. Then write your script so the optional stuff points to the extra handles. We'll

#!/bin/sh

exec 6>/dev/null

if [ $# -ge 1 -a "$1" = "--verbose" ]; then
echo    "Verbose mode."
exec 6>&1
fi

echo "Things I want to see regardless of my verbose switch."

#... Other things...

# This command and others along the script should only be seen if I am in verbose mode.
ls -l >6 2>&1

That should get you started. I'm not sure if that is BASH specific or not. It was just a memory of long ago. ;-)

share|improve this answer

I don't know about sh but in bash (not the same!) you'll need to use eval:

$ x='> foo'
$ echo Hi $x
Hi > foo
$ eval echo Hi $x
$ cat foo
Hi
share|improve this answer
    
I don't need it to go backward. I just want to have the evaluation going asortedly on across my script without need to ifing or evaling everywhere. This script is meant to run on a constrainded embedded device where scripts go really slow. I feel it must be a trick for this. Thanks! –  fljx Aug 6 '12 at 19:33

I believe your test is backwards. You want to redirect to /dev/null when not in verbose mode:

if [ $# -ge 1 -a "$1" = "--verbose" ]; then
    echo    "Verbose mode."
else
    REDIRECT='2>&1 >/dev/null'
fi
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.