Path: csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Chris Elvidge Newsgroups: gnu.bash.bug Subject: Re: Return from function depending on number of parameters Date: Thu, 9 Jul 2020 17:48:41 +0100 Lines: 36 Approved: bug-bash@gnu.org Message-ID: References: <20200706115026.GV22833@eeg.ccf.org> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: usenet.stanford.edu 1594313334 25094 209.51.188.17 (9 Jul 2020 16:48:54 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org Envelope-to: bug-bash@gnu.org X-Injected-Via-Gmane: http://gmane.org/ User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 In-Reply-To: <20200706115026.GV22833@eeg.ccf.org> Content-Language: en-GB Received-SPF: pass client-ip=116.202.254.214; envelope-from=gnu-bug-bash-3@m.gmane-mx.org; helo=ciao.gmane.io X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/09 12:48:47 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: 43 X-Spam_score: 4.3 X-Spam_bar: ++++ X-Spam_report: (4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_ADSP_CUSTOM_MED=0.001, FORGED_GMAIL_RCVD=1, FORGED_MUA_MOZILLA=2.309, FREEMAIL_FORGED_FROMDOMAIN=1, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=1, NML_ADSP_CUSTOM_MED=0.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <20200706115026.GV22833@eeg.ccf.org> Xref: csiph.com gnu.bash.bug:16555 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