From: #! /shell/nerd on
Hi,

I have a function MyFunc() and it prints some stuff & always returns
0.
In order to suppress it I'm redirecting STDOUT to NULL. Now when I try

a. MyFunc >&-
ExitStatus=$?
I get the ExitStatus as 1. Where when I try

b. MyFunc >/dev/null
ExitStatus=$?
I get the ExitStatus as 0.
Why ??
From: Chris F.A. Johnson on
On 2010-01-16, #! /shell/nerd wrote:
> Hi,
>
> I have a function MyFunc() and it prints some stuff & always returns
> 0.
> In order to suppress it I'm redirecting STDOUT to NULL. Now when I try
>
> a. MyFunc >&-
> ExitStatus=$?
> I get the ExitStatus as 1. Where when I try
>
> b. MyFunc >/dev/null
> ExitStatus=$?
> I get the ExitStatus as 0.
> Why ??

Did you not get an error messsage saying that you had a bad file
descriptor?

'>&-' is incorrect.

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====
From: #! /shell/nerd on
On Jan 15, 10:40 pm, "Chris F.A. Johnson" <cfajohn...(a)gmail.com>
wrote:
> On 2010-01-16, #! /shell/nerd wrote:
>
> > Hi,
>
> > I have a function MyFunc() and it prints some stuff & always returns
> > 0.
> > In order to suppress it I'm redirecting STDOUT to NULL. Now when I try
>
> > a. MyFunc >&-
> >    ExitStatus=$?
> > I get the ExitStatus as 1. Where when I try
>
> > b. MyFunc >/dev/null
> >    ExitStatus=$?
> > I get the ExitStatus as 0.
> > Why ??
>
>    Did you not get an error messsage saying that you had a bad file
>    descriptor?
>
>    '>&-' is incorrect.
>
> --
>    Chris F.A. Johnson, author           <http://shell.cfajohnson.com/>
>    ===================================================================
>    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
>    Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
>    ===== My code in this post, if any, assumes the POSIX locale  =====
>    ===== and is released under the GNU General Public Licence    =====

Yeah I did. But somewhere I read that we can do it.
From: Barry Margolin on
In article
<e5c4e76d-0413-4ab7-997d-523359d6a411(a)l19g2000yqb.googlegroups.com>,
"#! /shell/nerd" <vikasera(a)gmail.com> wrote:

> Hi,
>
> I have a function MyFunc() and it prints some stuff & always returns
> 0.
> In order to suppress it I'm redirecting STDOUT to NULL. Now when I try
>
> a. MyFunc >&-
> ExitStatus=$?
> I get the ExitStatus as 1. Where when I try
>
> b. MyFunc >/dev/null
> ExitStatus=$?
> I get the ExitStatus as 0.
> Why ??

A function's exit status is the exit status of the last command in it.
I presume that the last thing your function does is try to print
something. Since you've closed stdout, this gets an error, and that
error is propagated as the exit status of the function.

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Icarus Sparry on
On Sat, 16 Jan 2010 04:40:39 +0000, Chris F.A. Johnson wrote:

> On 2010-01-16, #! /shell/nerd wrote:
>> Hi,
>>
>> I have a function MyFunc() and it prints some stuff & always returns 0.
>> In order to suppress it I'm redirecting STDOUT to NULL. Now when I try
>>
>> a. MyFunc >&-
>> ExitStatus=$?
>> I get the ExitStatus as 1. Where when I try
>>
>> b. MyFunc >/dev/null
>> ExitStatus=$?
>> I get the ExitStatus as 0.
>> Why ??
>
> Did you not get an error messsage saying that you had a bad file
> descriptor?
>
> '>&-' is incorrect.

The redirection >&- is a perfectly good redirection, it is the short form
of 1>&-, which says to close file descriptor 1 aka stdout.

Barry has already explained why this probably causes the function to
return an error indication.