Verbose output vs. discarding output

Verbose output vs. discarding output

Post by Vikas Agnihotr » Wed, 03 Mar 2004 10:08:27



Shell is ksh

I want to add a [-v] switch to a shell function to either be verbose or,
in the absense of this switch, send all stdout and stderr to /dev/null.

What is the best practice for this?

Something like

[Use getopts or regular command line processing to set $verbose]

[[ ! -z $verbose ]] && output=/dev/fd/0 || output=/dev/null
exec 1>$output 2>&1
...

Does the 'exec' apply to the calling script as well? (I dont want it to)

Any other way to do this? Thanks

 
 
 

Verbose output vs. discarding output

Post by Alexis Huxle » Wed, 03 Mar 2004 18:35:00


Quote:> I want to add a [-v] switch to a shell function to either be verbose or,
> in the absense of this switch, send all stdout and stderr to /dev/null.

> What is the best practice for this?

> Something like

> [Use getopts or regular command line processing to set $verbose]

> [[ ! -z $verbose ]] && output=/dev/fd/0 || output=/dev/null
> exec 1>$output 2>&1
> ...

> Does the 'exec' apply to the calling script as well? (I dont want it to)

I would say that messages should not globally be redirected to some
other location, but rather redirected on a per-message basis.

Think about a program that - in all situations - should produce some
output! Ok, stupid example, imagine a 'myDate' command that without
'-v' just says:

        Tue Mar  2 10:19:31 CET 2004

but with it says:

        myDate: INFO: opening TCP/IP connection to atomic clock ...
        myDate: INFO: requesting date ...
        Tue Mar  2 10:19:31 CET 2004
        myDate: INFO: closing connection ...

Also, the verbose messages should probably go to stderr not stdout, and
the proper output to stdout (see further down for the advantages).

So I have a function like this:

        message_info()
        {
            [ $OPT_VERBOSITY -lt 3 ] || {
                echo "$PROGNAME: INFO: $1" >&2
            }
        }

and then in the shell script I say:

        message_info "opening TCP/IP connection to atomic clock ..."
        message_info "requesting date ..."
        doTheRealStuffHere
        message_info "closing connecting ..."

Note that when the stderr is redirected away or the '-v' option is not
used then the output differs in no way from normal.

Similarly I have debug, warning, error functions, which only
display their message of $OPT_VERBOSITY is greater than or equal to
<first-param-to-debug-call>, 2, 1 respectively.

Then '-v' raises OPT_VERBOSITY from the default of 2 to 3.

Also note that when I redirect stdout with something like:

        DATE=`myDate`

then any error message, also sent to stderr, will *still* be seen
by the user.

Alexis

 
 
 

1. verbose last output?

I need to get the last program to extract more than 16 characters
of the incoming hostname or IP address. I have looked in several
manuals and the source code I could find, but have not been able
to figure out how to get last to extract more than 16 characters.

Response by e-mail is much appreciated. Though not an emergency
situation, the ability to determine the full source address of
every user's connection would be helpful. Please send your advice
if you know how to get this information out of the wtmp/wtmpx files
in Solaris 2.5.5. Thanks.

Mark Hedges
Infonex Internet Services

2. probs w/ X Config in Mandrake

3. natd in verbose mode: strange output

4. Wanted: Solaris 2.3 config files for Motif 1.2.1

5. How do I got more verbose output with sendmail?

6. ISDN overfllow stack ?

7. MPICH to display verbose SSH output

8. Netscape broken under 1.3.70

9. kbuild: Smart notation for non-verbose output

10. cannot get all verbose ksh output into a log file

11. stty -g output mapping to stty -a output

12. Repost of "Output redirection while reformatting the output"

13. Perl script log file output has duplicate copies of output lines