Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

For God's sake, stop researching for a while and begin to think!


devel / comp.unix.shell / Re: double-quotes in arithmetic expressions

SubjectAuthor
* random case construct in kshHelmut Waitzmann
`* random case construct in kshJanis Papanagnou
 `* double-quotes in arithmetic expressions (was: random case construct in ksh)Helmut Waitzmann
  `- double-quotes in arithmetic expressionsJanis Papanagnou

1
Re: random case construct in ksh

<83iln12vj1.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6601&group=comp.unix.shell#6601

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!jGJWRQBHlGg7y8HJ7OzaIg.user.46.165.242.75.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: random case construct in ksh
Date: Tue, 09 Aug 2022 19:09:54 +0200
Organization: Aioe.org NNTP Server
Message-ID: <83iln12vj1.fsf@helmutwaitzmann.news.arcor.de>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<jlensnFe6foU1@mid.individual.net> <tct9t1$1bq6k$1@dont-email.me>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="47891"; posting-host="jGJWRQBHlGg7y8HJ7OzaIg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:k1kWpOpxpw3s7PzixE4jFRGQAmE=
Mail-Copies-To: nobody
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
X-Notice: Filtered by postfilter v. 0.9.2
 by: Helmut Waitzmann - Tue, 9 Aug 2022 17:09 UTC

Janis Papanagnou <janis_papanagnou@hotmail.com>:
>On 09.08.2022 11:32, Martijn Dekker wrote:
>> Op 06-08-22 om 19:13 schreef Janis Papanagnou:
>>> For readability I prefer the [non-standard] $(( RANDOM % 3 ))
>>
>> FYI, referring to variables without a leading $ in arithmetic
>> expressions is perfectly standard:
>>
>> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04
>>
>> | If the shell variable x contains a value that forms a valid integer
>> | constant, optionally including a leading <plus-sign> or <hyphen-
>> | minus>, then the arithmetic expansions "$((x))" and "$(($x))" shall
>> | return the same value.
>
>Oh, thanks! - Has that changed? (I seem to recall that last time I
>looked at that issue it didn't support $-less variables in arithmetic
>expressions.) - Anyway, good to know.

There might be a difference, though, if the value of the variable is
not a literal integer, but a string resembling an integer
expression.  Try, using different shells, the following commands:

v='6*7'
printf '%s\n' "$((v))"
printf '%s\n' "$((${v}))"

Re: random case construct in ksh

<tcvr79$1papg$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6605&group=comp.unix.shell#6605

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: random case construct in ksh
Date: Wed, 10 Aug 2022 10:45:28 +0200
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <tcvr79$1papg$1@dont-email.me>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<jlensnFe6foU1@mid.individual.net> <tct9t1$1bq6k$1@dont-email.me>
<83iln12vj1.fsf@helmutwaitzmann.news.arcor.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 10 Aug 2022 08:45:29 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="36dd0e9b16a7cf2815e9b7db1df5f489";
logging-data="1878832"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GUxHJnCzAUgf8Iudd81xo"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:a/Y+4gjiqfgaZ5aJr70NwXdUDbs=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <83iln12vj1.fsf@helmutwaitzmann.news.arcor.de>
 by: Janis Papanagnou - Wed, 10 Aug 2022 08:45 UTC

On 09.08.2022 19:09, Helmut Waitzmann wrote:
> Janis Papanagnou <janis_papanagnou@hotmail.com>:
>> On 09.08.2022 11:32, Martijn Dekker wrote:
>>> Op 06-08-22 om 19:13 schreef Janis Papanagnou:
>>>> For readability I prefer the [non-standard] $(( RANDOM % 3 ))
>>>
>>> FYI, referring to variables without a leading $ in arithmetic
>>> expressions is perfectly standard:
>>>
>>> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04
>>>
>>>
>>> | If the shell variable x contains a value that forms a valid integer
>>> | constant, optionally including a leading <plus-sign> or <hyphen-
>>> | minus>, then the arithmetic expansions "$((x))" and "$(($x))" shall
>>> | return the same value.
>>
>> Oh, thanks! - Has that changed? (I seem to recall that last time I
>> looked at that issue it didn't support $-less variables in arithmetic
>> expressions.) - Anyway, good to know.
>
> There might be a difference, though, if the value of the variable is not
> a literal integer, but a string resembling an integer expression. Try,
> using different shells, the following commands:
>
> v='6*7'
> printf '%s\n' "$((v))"
> printf '%s\n' "$((${v}))"

I anyway wouldn't have expected that formulas in string variables
would have been handled (like ksh/bash/zsh do) in the first place.

A probably more interesting case (where these three shells differ)
is
printf '%s\n' $(("${v}"))

Janis

double-quotes in arithmetic expressions (was: random case construct in ksh)

<8335e417ib.fsf_-_@helmutwaitzmann.news.arcor.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6612&group=comp.unix.shell#6612

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!6TQt5PeKZbpJyBy6K1CKvg.user.46.165.242.75.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: double-quotes in arithmetic expressions (was: random case construct in ksh)
Date: Wed, 10 Aug 2022 16:46:20 +0200
Organization: Aioe.org NNTP Server
Message-ID: <8335e417ib.fsf_-_@helmutwaitzmann.news.arcor.de>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<jlensnFe6foU1@mid.individual.net> <tct9t1$1bq6k$1@dont-email.me>
<83iln12vj1.fsf@helmutwaitzmann.news.arcor.de>
<tcvr79$1papg$1@dont-email.me>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="41992"; posting-host="6TQt5PeKZbpJyBy6K1CKvg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:u5i+ap6p+oPrSWNSdV+8orxk/uY=
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mail-Copies-To: nobody
 by: Helmut Waitzmann - Wed, 10 Aug 2022 14:46 UTC

Janis Papanagnou <janis_papanagnou@hotmail.com>:
>On 09.08.2022 19:09, Helmut Waitzmann wrote:
>> v='6*7'
>> printf '%s\n' "$((v))"
>> printf '%s\n' "$((${v}))"
>
>I anyway wouldn't have expected that formulas in string variables
>would have been handled (like ksh/bash/zsh do) in the first place.
>
>A probably more interesting case (where these three shells differ)
>is
> printf '%s\n' $(("${v}"))

POSIX
(<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04>)
says:

$((expression))

The expression shall be treated as if it were in
double-quotes, except that a double-quote inside the
expression is not treated specially. The shell shall
expand all tokens in the expression for parameter
expansion, command substitution, and quote removal.

Does "a double-quote inside the expression is not treated specially"
say that double-quotes are treated literally, i.e. excluded from
quote removal, thus left as (a syntactic invalid) part of the
arithmetic expression?

Re: double-quotes in arithmetic expressions

<td0jrf$1sg6s$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=6613&group=comp.unix.shell#6613

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: double-quotes in arithmetic expressions
Date: Wed, 10 Aug 2022 17:45:51 +0200
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <td0jrf$1sg6s$1@dont-email.me>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<jlensnFe6foU1@mid.individual.net> <tct9t1$1bq6k$1@dont-email.me>
<83iln12vj1.fsf@helmutwaitzmann.news.arcor.de> <tcvr79$1papg$1@dont-email.me>
<8335e417ib.fsf_-_@helmutwaitzmann.news.arcor.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 10 Aug 2022 15:45:51 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="36dd0e9b16a7cf2815e9b7db1df5f489";
logging-data="1982684"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uW93ws25Q0r2sxQy3Kbqt"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:9BZ0nozpZ/gyMs2RTlYOaHRrGA0=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <8335e417ib.fsf_-_@helmutwaitzmann.news.arcor.de>
 by: Janis Papanagnou - Wed, 10 Aug 2022 15:45 UTC

On 10.08.2022 16:46, Helmut Waitzmann wrote:
> Janis Papanagnou <janis_papanagnou@hotmail.com>:
>> On 09.08.2022 19:09, Helmut Waitzmann wrote:
>>> v='6*7'
>>> printf '%s\n' "$((v))"
>>> printf '%s\n' "$((${v}))"
>>
>> I anyway wouldn't have expected that formulas in string variables
>> would have been handled (like ksh/bash/zsh do) in the first place.
>>
>> A probably more interesting case (where these three shells differ) is
>> printf '%s\n' $(("${v}"))
>
> POSIX
> (<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04>)
> says:
>
> $((expression))
>
> The expression shall be treated as if it were in
> double-quotes, except that a double-quote inside the
> expression is not treated specially. The shell shall
> expand all tokens in the expression for parameter
> expansion, command substitution, and quote removal.
>
> Does "a double-quote inside the expression is not treated specially" say
> that double-quotes are treated literally, i.e. excluded from quote
> removal, thus left as (a syntactic invalid) part of the arithmetic
> expression?

My interpretation would be that it's effectively just ignored because
the expression is anyway treated like being double-quoted. It would,
IMO, also make no sense to assume differently. Throw an error for a
quoted $-expression?!

But shells behave differently, so maybe it's not that clear...

$ ksh -c 'v="6*7" ; printf "%s\n" "$((v))" ; printf "%s\n" $(("${v}"))'
42
42

$ zsh -c 'v="6*7" ; printf "%s\n" "$((v))" ; printf "%s\n" $(("${v}"))'
42
zsh:1: bad math expression: illegal character: "

$ bash -c 'v="6*7" ; printf "%s\n" "$((v))" ; printf "%s\n" $(("${v}"))'
42
bash: "6*7": syntax error: operand expected (error token is ""6*7"")

At least bash and zsh seem to interpret it the way you suspected. (And
as so often, I'm happy to use ksh here which serves my interpretation.)

And dash complains completely...

$ dash -c 'v="6*7" ; printf "%s\n" "$((v))" ; printf "%s\n" $(("${v}"))'
dash: 1: Illegal number: 6*7

Janis

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor