Unix Technical Forum

shell programming - conditional redirect STDOUT

This is a discussion on shell programming - conditional redirect STDOUT within the Sco Unix forums, part of the Unix Operating Systems category; --> using /bin/sh (or bash), i'd like to conditionally redirect STDOUT of a shell script or program being called from ...


Go Back   Unix Technical Forum > Unix Operating Systems > Sco Unix

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-15-2008, 06:04 PM
Joe Chasan
 
Posts: n/a
Default shell programming - conditional redirect STDOUT

using /bin/sh (or bash), i'd like to conditionally redirect STDOUT
of a shell script or program being called from another shell script,
and decide this from within that shell script.

an over simplified example of the problematic part would be:
dodo=">/dev/null"
ls -l $dodo

where i'd want output of ls -l to be sent to /dev/null
but the above yields:
ls: >/dev/null not found: No such file or directory (error 2)

i'm sure i'm missing an easier way of doing it.

--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ---
-Joe Chasan- Magnatech Business Systems, Inc.
joe@magnatechonline.com Hicksville, NY - USA
http://www.MagnatechOnline.com Tel.(516) 931-4444/Fax.(516) 931-1264
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-15-2008, 06:04 PM
XeniXman
 
Posts: n/a
Default Re: shell programming - conditional redirect STDOUT


Joe Chasan wrote:
> using /bin/sh (or bash), i'd like to conditionally redirect STDOUT
> of a shell script or program being called from another shell script,
> and decide this from within that shell script.
>
> an over simplified example of the problematic part would be:
> dodo=">/dev/null"
> ls -l $dodo
>
> where i'd want output of ls -l to be sent to /dev/null
> but the above yields:
> ls: >/dev/null not found: No such file or directory (error 2)
>
> i'm sure i'm missing an easier way of doing it.
>
> --- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ---
> -Joe Chasan- Magnatech Business Systems, Inc.
> joe@magnatechonline.com Hicksville, NY - USA
> http://www.MagnatechOnline.com Tel.(516) 931-4444/Fax.(516) 931-1264


Try:

dodo=">/dev/null"
eval ls -l $dodo

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-15-2008, 06:04 PM
Brian K. White
 
Posts: n/a
Default Re: shell programming - conditional redirect STDOUT


----- Original Message -----
From: "Joe Chasan" <joe@magnatechonline.com>
Newsgroups: comp.unix.sco.misc
To: <distro@jpr.com>
Sent: Thursday, June 22, 2006 9:39 AM
Subject: shell programming - conditional redirect STDOUT


> using /bin/sh (or bash), i'd like to conditionally redirect STDOUT
> of a shell script or program being called from another shell script,
> and decide this from within that shell script.
>
> an over simplified example of the problematic part would be:
> dodo=">/dev/null"
> ls -l $dodo
>
> where i'd want output of ls -l to be sent to /dev/null
> but the above yields:
> ls: >/dev/null not found: No such file or directory (error 2)
>
> i'm sure i'm missing an easier way of doing it.


DEST=null
[ <condition> ] && DEST=tty

ls -l >/dev/$DEST

Or you can fill DEST with the whole "/dev/null" so that conditionally it can
be "/path/to/file"
What you can't do is put the redirector into the variable so you can't
conditionally choose to pipe into another program instead of redirect or
append.

then it would have to be

[ <condition> ] && {
ls -l >/dev/null
:
} || {
ls -l |otherscript
}


or maybe

TF=/tmp/file$$
rm -f $TF >/dev/null 2>&1
case $condition in
a) DEST=/dev/tty # show on screen
b) DEST=$TF # capture for otherscript
*) DEST=/dev/null # default silent
esac
ls -l >$DEST

# if $TF exists and is not zero bytes, then feed it to otherscript
[ -s $TF ] && otherscript < $TF
rm -f $TF


If you need to capture it or pipe it AND want to see it on screen too, then
you want the tee command.

Brian K. White -- brian@aljex.com -- http://www.aljex.com/bkw/
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx Linux SCO FreeBSD #callahans Satriani Filk!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-15-2008, 06:04 PM
Joe Chasan
 
Posts: n/a
Default Re: shell programming - conditional redirect STDOUT

On Thu, Jun 22, 2006 at 10:45:42AM -0400, Brian K. White wrote:
>
> ----- Original Message -----
> From: "Joe Chasan" <joe@magnatechonline.com>
> Newsgroups: comp.unix.sco.misc
> To: <distro@jpr.com>
> Sent: Thursday, June 22, 2006 9:39 AM
> Subject: shell programming - conditional redirect STDOUT
>
>
> > using /bin/sh (or bash), i'd like to conditionally redirect STDOUT
> > of a shell script or program being called from another shell script,
> > and decide this from within that shell script.
> >
> > an over simplified example of the problematic part would be:
> > dodo=">/dev/null"
> > ls -l $dodo
> >
> > where i'd want output of ls -l to be sent to /dev/null
> > but the above yields:
> > ls: >/dev/null not found: No such file or directory (error 2)
> >
> > i'm sure i'm missing an easier way of doing it.

>
> DEST=null
> [ <condition> ] && DEST=tty
>
> ls -l >/dev/$DEST
>
> Or you can fill DEST with the whole "/dev/null" so that conditionally it can
> be "/path/to/file"
> What you can't do is put the redirector into the variable so you can't
> conditionally choose to pipe into another program instead of redirect or
> append.
>
> then it would have to be
>
> [ <condition> ] && {
> ls -l >/dev/null
> :
> } || {
> ls -l |otherscript
> }
>
>
> or maybe
>
> TF=/tmp/file$$
> rm -f $TF >/dev/null 2>&1
> case $condition in
> a) DEST=/dev/tty # show on screen
> b) DEST=$TF # capture for otherscript
> *) DEST=/dev/null # default silent
> esac
> ls -l >$DEST
>
> # if $TF exists and is not zero bytes, then feed it to otherscript
> [ -s $TF ] && otherscript < $TF
> rm -f $TF


thanks, that & xenixman's suggestion of eval would both work.
i was worried that if i sent to /dev/tty i would lose interactive
control but after testing your method it appears i was wrong.

--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ---
-Joe Chasan- Magnatech Business Systems, Inc.
joe@magnatechonline.com Hicksville, NY - USA
http://www.MagnatechOnline.com Tel.(516) 931-4444/Fax.(516) 931-1264
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-15-2008, 06:04 PM
Brian K. White
 
Posts: n/a
Default Re: shell programming - conditional redirect STDOUT

XenixMan:
> Try:
>
> dodo=">/dev/null"
> eval ls -l $dodo


Nice, I forgot all about eval.

Brian K. White -- brian@aljex.com -- http://www.aljex.com/bkw/
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx Linux SCO FreeBSD #callahans Satriani Filk!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-15-2008, 06:05 PM
John DuBois
 
Posts: n/a
Default Re: shell programming - conditional redirect STDOUT

In article <20060622093838.A19217@magnatechonline.com>,
Joe Chasan <joe@magnatechonline.com> wrote:
>using /bin/sh (or bash), i'd like to conditionally redirect STDOUT
>of a shell script or program being called from another shell script,
>and decide this from within that shell script.
>
>an over simplified example of the problematic part would be:
>dodo=">/dev/null"
>ls -l $dodo
>
>where i'd want output of ls -l to be sent to /dev/null
>but the above yields:
>ls: >/dev/null not found: No such file or directory (error 2)
>
>i'm sure i'm missing an easier way of doing it.


In addition to the other methods, most generically you can do something like this:

i-want-to-discard-stdout && exec 3>/dev/null || exec 3>&1
my-command 1>&3

This avoids the potential pitfalls of eval, or having to repeat a large/complex
command, and works regardless of what stdout is.

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:51 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com