From: kj on



I have a shell script that executes several commands in sequence,
all of which, by default, produce a lot of output, both to stdout
and stderr.

These commands are heterogeneous, and therefore do not have a common
mechanism for specifying verbosity levels; in fact, for some of
them, there is no such mechanism at all.

Of course, I can silence any one of them with something like

some_noisy_command >/dev/null 2>&1

....but I don't want to hard-code this indirection. Instead I would
like to have quiet operation by default, but support verbose
operation through a command-line option for the script.

I'm really out of my depth here. This naive approach fails (of
course):

[ -z $VERBOSE ] && DEVNULL='>/dev/null 2>&1'
some_noisy_command $DEVNULL

What's the right way to implement this verbosity level control?

(FWIW I use zsh.)

TIA!

~K
From: pk on
kj wrote:

> I have a shell script that executes several commands in sequence,
> all of which, by default, produce a lot of output, both to stdout
> and stderr.
>
> These commands are heterogeneous, and therefore do not have a common
> mechanism for specifying verbosity levels; in fact, for some of
> them, there is no such mechanism at all.
>
> Of course, I can silence any one of them with something like
>
> some_noisy_command >/dev/null 2>&1
>
> ...but I don't want to hard-code this indirection. Instead I would
> like to have quiet operation by default, but support verbose
> operation through a command-line option for the script.
>
> I'm really out of my depth here. This naive approach fails (of
> course):
>
> [ -z $VERBOSE ] && DEVNULL='>/dev/null 2>&1'
> some_noisy_command $DEVNULL
>
> What's the right way to implement this verbosity level control?
>
> (FWIW I use zsh.)


exec 3>&1 4>&2 1>/dev/null 2>/dev/null

....commands here...

exec 1>&3 2>&4 3>&- 4>&-

....normal I/O here ...
From: Bill Marcum on
On 2010-06-08, kj <no.email(a)please.post> wrote:
>
>
> I'm really out of my depth here. This naive approach fails (of
> course):
>
> [ -z $VERBOSE ] && DEVNULL='>/dev/null 2>&1'
> some_noisy_command $DEVNULL
>
> What's the right way to implement this verbosity level control?
>
eval some_noisy_command $DEVNULL


--
[It is] best to confuse only one issue at a time.
-- K&R
From: kj on
In <4286743.ObB369e8A3(a)xkzjympik> pk <pk(a)pk.invalid> writes:

>exec 3>&1 4>&2 1>/dev/null 2>/dev/null

>...commands here...

>exec 1>&3 2>&4 3>&- 4>&-

>...normal I/O here ...

I see. I always forget this use of exec. (It's a system call with
a split personality...)

Thanks!

~K
From: kj on
In <mkt2e7-fbl.ln1(a)marcumbill.bellsouth.net> Bill Marcum <marcumbill(a)bellsouth.net> writes:

>On 2010-06-08, kj <no.email(a)please.post> wrote:
>>
>>
>> I'm really out of my depth here. This naive approach fails (of
>> course):
>>
>> [ -z $VERBOSE ] && DEVNULL='>/dev/null 2>&1'
>> some_noisy_command $DEVNULL
>>
>> What's the right way to implement this verbosity level control?
>>
>eval some_noisy_command $DEVNULL

Thanks!

~K