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


Groups > gnu.bash.bug > #16531

Re: Return from function depending on number of parameters

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From Eli Schwartz <eschwartz@archlinux.org>
Newsgroups gnu.bash.bug
Subject Re: Return from function depending on number of parameters
Date Fri, 3 Jul 2020 18:16:32 -0400
Lines 102
Approved bug-bash@gnu.org
Message-ID <mailman.910.1593814610.2574.bug-bash@gnu.org> (permalink)
References <b1c19d38-64c0-f1ae-d08a-1ada435a0022@gmail.com> <4766f763-84b7-f018-f925-7c6390ed7523@archlinux.org>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="oMa14PW3WmI4ubb6JZrJfwR1jmC4lWBlp"
X-Trace usenet.stanford.edu 1593814612 14699 209.51.188.17 (3 Jul 2020 22:16:52 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
X-Spam-BL-Results
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/simple; d=archlinux.org; s=orion; t=1593814596; bh=rsNlpJjWu08c01tYQjVZNi3nsT7BsbkL7NjtdSaUeIE=; h=Subject:To:References:From:Date:In-Reply-To; b=llyCgdbp70jx//hFnCJcgRt8+IDhOHAn7+HdnqFmfvI29xhEvLfYA6S5zp+wKvzqJ 3EKXlIMwduqJhfw2SHUOyVrk5g+Zuej8owhRqVFnYgPmVaFpGost3aFIRykaivHeZ1 z/hYlHgHNnVyv8F59VCNwiVhY5lSNXf7BTZuVvpeFa69v3wXvrCWLOd64ReVjaAGas 6bMg0KnrubNEAArJEJYP9HADic4L607rQaHZV/w8C0lRUsXiw2xwHHPwkInFn7XAsT f/KOgxpzpI+E7heru3pS2bV2iSjsxgAFlqTjD8S7MMxKIpt/GESwmVnXfoPbhhgbHu a/3xBInLziAuztyOeLwoc2X/4SKHxeB8/WdN5DXsGi7eIPA6iduXhaQRQv0sCIUOJZ DyCRFdFkf4BjLNd/MIbjCSEmGgge1fjIsirCQIJfYgdfpAfogPkvSD1kOyPBYaBKAt v/dQc0R+Zlkoa09w8PJ66EJqi2erx919ytssBGEnj37S1aar6w+scXIG226lDPVoAB sr+Av3bv3IL+lFCRd7ByHv9NKjYPZVtVobr/GUZwUIoj9KMr25C42ZqApFZNQ41s8+ GaGi+fhMUOQCz1qXbq73KUh7Fr9Dub/FRTkjff6h4GVPCw0b0K1LX5HeBF2s82dTV7 3v1HJXQeR4d3hs3woO3y0BZg=
X-Clacks-Overhead GNU Terry Pratchett
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0
In-Reply-To <b1c19d38-64c0-f1ae-d08a-1ada435a0022@gmail.com>
Received-SPF pass client-ip=2a01:4f8:160:6087::1; envelope-from=eschwartz@archlinux.org; helo=orion.archlinux.org
X-detected-operating-system by eggs.gnu.org: No matching host in p0f cache. That's all we know.
X-Spam_score_int -53
X-Spam_score -5.4
X-Spam_bar -----
X-Spam_report (-5.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN
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 <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <4766f763-84b7-f018-f925-7c6390ed7523@archlinux.org>
X-Mailman-Original-References <b1c19d38-64c0-f1ae-d08a-1ada435a0022@gmail.com>
Xref csiph.com gnu.bash.bug:16531

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 7/3/20 2:00 PM, Chris Elvidge wrote:
> I've used 'return $((!$#))' and 'return $[!$#]' to return an error if no
> parameters given to function.
> 
> Tested in a bash script 'exit $((!$#)) / $[!$#]' - both work.
> 
> 'echo  $((!$#)) / $[!$#]' - both echo 1 when no params, 0 when any
> number of params.
> 
> I'm told ( https://wiki.bash-hackers.org/scripting/obsolete ) that
> $[...] is obsolete and that $((...)) should be used instead. OK so far.
> 
> However 'N=0; echo $((!$N))' gives an error at the bash prompt. 'echo
> $[!$N]' echo's 1 as expected.

"gives an error" is a useless bug report. It works for me.

$ N=0; echo $((!$N))
1

My initial reaction to reading this thread is head scratching!

As the other reply mentioned, there's actually a good explanation for
why we get different results -- I disabled an annoying feature.

$ set -o histexpand

Now here's a useful bug report. "When I run this, I get the following
incorrect results or error message":

$ N=0; echo $((!$N))
N=0; echo $((histexpandN))
0
$ N=0; echo $((!$N))
N=0; echo $(()N))
-bash: syntax error near unexpected token `)'
$ N=0
$ echo $((!$N))
echo $((N=0N))
-bash: N=0N: value too great for base (error token is "0N")

...

From there, people can give useful advice for solving the problem. (My
preferred advice is "disable histexpand".)

-- 
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

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


Thread

Re: Return from function depending on number of parameters Eli Schwartz <eschwartz@archlinux.org> - 2020-07-03 18:16 -0400

csiph-web