Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16555

Re: Return from function depending on number of parameters

From Chris Elvidge <celvidge001@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: Return from function depending on number of parameters
Date 2020-07-09 17:48 +0100
Message-ID <mailman.5.1594313334.2306.bug-bash@gnu.org> (permalink)
References <b1c19d38-64c0-f1ae-d08a-1ada435a0022@gmail.com> <20200706115026.GV22833@eeg.ccf.org> <re7hp9$ids$1@ciao.gmane.io>

Show all headers | View raw


On 06/07/2020 12:50 pm, Greg Wooledge wrote:
> On Fri, Jul 03, 2020 at 07:00:54PM +0100, Chris Elvidge wrote:
>> I've used 'return $((!$#))' and 'return $[!$#]' to return an error if no
>> parameters given to function.
> 
> The problem with this is that it *always* returns from the function,
> even when paramters are given.
> 
> If you actually want to do what you said, you need a conditional check
> of some kind, either "if" or "||".
> 
> (($#)) || return 1
> 
> would be my preference, if you really do not want to give any kind of
> error message to indicate *why* you are returning with a failure code.
> 
> If you want to add an error message, then I would go with "if".
> 
> if ((! $#)); then
>    echo "usage: myfunc arg1 ..." >&2
>    return 1
> fi
> 
> 

This is a function only for me. The return comes after a help section.
[[ $# -eq 0 || "$1" =~ -h ]] &&  etc. No error message required, just an 
indication.

Thanks for the pointers.

-- 
Chris Elvidge
England

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: Return from function depending on number of parameters Chris Elvidge <celvidge001@gmail.com> - 2020-07-09 17:48 +0100

csiph-web