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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 |
| |||
| 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 |
| |||
| ----- 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! |
| |||
| 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 |
| |||
| 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! |
| ||||
| 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/ |
| Thread Tools | |
| Display Modes | |
|
|