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


Groups > gnu.bash.bug > #16540

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 Sat, 4 Jul 2020 23:35:57 -0400
Lines 131
Approved bug-bash@gnu.org
Message-ID <mailman.991.1593920179.2574.bug-bash@gnu.org> (permalink)
References <b1c19d38-64c0-f1ae-d08a-1ada435a0022@gmail.com> <4766f763-84b7-f018-f925-7c6390ed7523@archlinux.org> <73b2d4c9-badf-e059-3a77-57d2f9ef197b@gmail.com> <f30e986c-81fd-dc72-a726-c256ee94e75a@archlinux.org>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="3JfJBiAxK4ZFqZ9fUTMIO1MTXjNrWjlSx"
X-Trace usenet.stanford.edu 1593920180 15189 209.51.188.17 (5 Jul 2020 03:36:20 GMT)
X-Complaints-To action@cs.stanford.edu
To Chris Elvidge <celvidge001@gmail.com>, 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=1593920162; bh=By8qHi45aGa6R0xI+wxt0Tv+N1gPc5/5kMWkB57nR50=; h=Subject:To:References:From:Date:In-Reply-To; b=N8d6VlCh6gHPfIvZlQq1moaDiqLLL8vOOTNanRaD3NBg/zqBVg9kSJhozgbOGt0Mh 2wd2ODME+G6fhNNCh0XCMG1Xo2HP91/IB4hZhezmjcRqHro441qoZARvKE6vSuyZmk kkVwazTSujt0UQ1O7diLmcTMGqekWm7uHzTwdZpik4Qjl9SEcb7Ldz2auJ0zzsJgcy 4EH/37kWjpReTpj8qUr6q7MP8IEXjNlFR1+3WB3PH8Z884Gl3+PfxLuhUarP47ZmG0 /n6C99UluoKT4dTHC/6ypo8/tIkck+5jc70zOECQQL2/VpW9w2/duDtcTsO9SkR8V4 VTrmUVdtkuEajNHHQKj+pqnPLUlfbFn53Q0bGF3DLx5yftuSarygM0PPRoyQ4as1nk Nrg0fL3ELYFgLKrOksirf8eVr1eD5S/h6BtrP/bPVySAzc9OeBOGeSCs/MzQBwSW8K efJ65GKmXMuw44r2BkjyOI07rRUn+VVvYR26nCHJGUQlraC28cgR8mmUb52v2WZcI0 So5C7Qsaj8aNByr5OhWxPg05tSCyGrvDfl7MgAmrxbaBKFU9EK3BCVb8IktWbQmyMv Als6s/fVfiPVSQMI1gcry4YDHMhzikM/bowZMMtsM9mzCSZVvx38EgIwaCbsVE64a4 v5EvYxvJjVx3MLz06uKAmnLc=
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 <73b2d4c9-badf-e059-3a77-57d2f9ef197b@gmail.com>
Received-SPF pass client-ip=88.198.91.70; envelope-from=eschwartz@archlinux.org; helo=orion.archlinux.org
X-detected-operating-system by eggs.gnu.org: First seen = 2020/07/04 23:36:09
X-ACL-Warn Detected OS = Linux 2.2.x-3.x [generic] [fuzzy]
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, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, 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 <f30e986c-81fd-dc72-a726-c256ee94e75a@archlinux.org>
X-Mailman-Original-References <b1c19d38-64c0-f1ae-d08a-1ada435a0022@gmail.com> <4766f763-84b7-f018-f925-7c6390ed7523@archlinux.org> <73b2d4c9-badf-e059-3a77-57d2f9ef197b@gmail.com>
Xref csiph.com gnu.bash.bug:16540

Show key headers only | View raw


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

On 7/4/20 7:54 AM, Chris Elvidge wrote:
> On 03/07/2020 11:16 pm, Eli Schwartz wrote:
>> 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".)
>>
> 
> Thanks for the pointers to a better bug report.
> And thanks for the info on histexpand. I obviously should read the
> manual more carefully - I'd never come across it - as it's set by
> default, not in any profile or bashrc file.

It's the section "HISTORY EXPANSION", and set -o histexpand is
equivalent to set -H, which controls this option.

Many people first realize it exists only when they've been bitten by it. :)

> Did you mean set +o histexpand to disable the feature?

"set +o histexpand" is in my bashrc to disable the feature.

"set -o histexpand" is what I used at the console to temporarily
re-enable it, in order to reproduce your issue. As you can see from

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

the first history expansion inserted the word "histexpand" into my
command, because the previous command used the word.

-- 
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-04 23:35 -0400

csiph-web