Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

7 May, 2024: fms is rebuilding. Don't expect much in that section for quite a few days, maybe longer.


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

SubjectAuthor
* random case construct in kshLaurens Kils-Hütten
`* Re: random case construct in kshJanis Papanagnou
 +* Re: random case construct in kshLaurens Kils-Hütten
 |+* Re: random case construct in kshJanis Papanagnou
 ||`* Re: random case construct in kshJanis Papanagnou
 || `- Re: random case construct in kshLaurens Kils-Hütten
 |`* Re: random case construct in kshGeoff Clare
 | `* RANDOM numbers in shell (Was: random case construct in ksh)Kenny McCormack
 |  +- Re: RANDOM numbers in shell (Was: random case construct in ksh)Christian Weisgerber
 |  `* Re: RANDOM numbers in shellLaurens Kils-Hütten
 |   +- Re: RANDOM numbers in shellKenny McCormack
 |   `- Re: RANDOM numbers in shellGeoff Clare
 `* Re: random case construct in kshMartijn Dekker
  `* Re: random case construct in kshJanis Papanagnou
   +- Re: random case construct in kshGeoff Clare
   `* Re: random case construct in kshHelmut Waitzmann
    `* Re: random case construct in kshJanis Papanagnou
     `* double-quotes in arithmetic expressions (was: random case construct in ksh)Helmut Waitzmann
      `- Re: double-quotes in arithmetic expressionsJanis Papanagnou

1
random case construct in ksh

<tcm782$12hr$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5535&group=comp.unix.shell#5535

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!sOAiplyedoQL/hL0V8UI7Q.user.46.165.242.75.POSTED!not-for-mail
From: lkh@sdf-eu.org (Laurens Kils-Hütten)
Newsgroups: comp.unix.shell
Subject: random case construct in ksh
Date: Sat, 6 Aug 2022 17:09:22 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tcm782$12hr$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="35387"; posting-host="sOAiplyedoQL/hL0V8UI7Q.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (NetBSD/9.1 (amd64))
X-Notice: Filtered by postfilter v. 0.9.2
 by: Laurens Kils-Hütten - Sat, 6 Aug 2022 17:09 UTC

In bash I have used a construct like the following to pick from a
number of cases at random. How would I do the same thing in ksh?

```bash

case $(($RANDOM%3)) in

0) echo "case 1" ;;

1) echo "case 2" ;;

2) echo "case 3" ;;

esac

```

thanks,

lkh

--
Laurens Kils-Hütten
https://sdf-eu.org/~lkh

Re: random case construct in ksh

<tcmb08$nlm$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5536&group=comp.unix.shell#5536

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.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: Sat, 6 Aug 2022 20:13:27 +0200
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <tcmb08$nlm$1@dont-email.me>
References: <tcm782$12hr$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 6 Aug 2022 18:13:28 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="8a7a5b57d4609ba2816f727dfb36b36b";
logging-data="24246"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18gl+Dm/ns8nw/wV6gt786+"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:uS7zRxGDOkjBDS7aNktSEFa12KA=
In-Reply-To: <tcm782$12hr$1@gioia.aioe.org>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 6 Aug 2022 18:13 UTC

On 06.08.2022 19:09, Laurens Kils-Hütten wrote:
> In bash I have used a construct like the following to pick from a
> number of cases at random. How would I do the same thing in ksh?

The case construct is standard and works in all POSIX shells
as it is. In ksh, therefore, you can write it the same way.
The only thing you probably cannot rely on in POSIX shells is
the RANDOM variable (which ksh of course supports).

For readability I prefer the [non-standard] $(( RANDOM % 3 ))
and I also write the tags (as the standard allows) in symmetric
parentheses like: (2) echo ... ;;

Janis

>
> ```bash
>
> case $(($RANDOM%3)) in
>
> 0) echo "case 1" ;;
>
> 1) echo "case 2" ;;
>
> 2) echo "case 3" ;;
>
> esac
>
> ```
>
> thanks,
>
> lkh
>

Re: random case construct in ksh

<tcqaim$1lt8$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5574&group=comp.unix.shell#5574

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!sOAiplyedoQL/hL0V8UI7Q.user.46.165.242.75.POSTED!not-for-mail
From: lkh@sdf-eu.org (Laurens Kils-Hütten)
Newsgroups: comp.unix.shell
Subject: Re: random case construct in ksh
Date: Mon, 8 Aug 2022 06:30:46 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tcqaim$1lt8$1@gioia.aioe.org>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
Injection-Info: gioia.aioe.org; logging-data="55208"; posting-host="sOAiplyedoQL/hL0V8UI7Q.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (NetBSD/9.1 (amd64))
X-Notice: Filtered by postfilter v. 0.9.2
 by: Laurens Kils-Hütten - Mon, 8 Aug 2022 06:30 UTC

Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
> The only thing you probably cannot rely on in POSIX shells is
> the RANDOM variable (which ksh of course supports).

Yes, that was precisely the problem. Also, I learned that /bin/sh and
/bin/ksh are not the same on netbsd ;-)

In /bin/sh this works:

#/bin/sh

case $(( $$ %3)) in

0) echo "case 1" ;;

1) echo "case 2" ;;

2) echo "case 3" ;;

esac

--
Have this adventurer for your next game:
Rosalie is a farmer
STR 14 INT 10 WIS 9 DEX 9 CON 10 CHA 11
HP 5 90 gp

Re: random case construct in ksh

<tcqoqq$tk1l$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5580&group=comp.unix.shell#5580

  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: Mon, 8 Aug 2022 12:34:02 +0200
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <tcqoqq$tk1l$1@dont-email.me>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<tcqaim$1lt8$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 8 Aug 2022 10:34:02 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="27c2ceb6b6e6723b55b4b4475b9a77ef";
logging-data="970805"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19dk02f7sh6wMf6DHO9QsEo"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Lk12+8zz42cCUElg9KgXPNg5QBo=
In-Reply-To: <tcqaim$1lt8$1@gioia.aioe.org>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Mon, 8 Aug 2022 10:34 UTC

On 08.08.2022 08:30, Laurens Kils-Hütten wrote:
> Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
>> The only thing you probably cannot rely on in POSIX shells is
>> the RANDOM variable (which ksh of course supports).
>
> Yes, that was precisely the problem. Also, I learned that /bin/sh and
> /bin/ksh are not the same on netbsd ;-)

But ksh is a (extreme) superset of sh, so you shouldn't have issues
as long as you program in POSIX or if you define ksh (or bash, etc.)
in the first place, if you want to use some of the modern features.

On legacy systems /bin/sh might be old bourne shell, but typically
you have a POSIX shell defined as /bin/sh nowadays. Other shells
may lie behind the somewhat generic "sh"; shells like ksh or bash
can often be found, but also other shells that (basically) conform
to POSIX. It is quite safe to assume any POSIX shell behind /bin/sh.

>
> In /bin/sh this works:

I don't see why that shouldn't work in other POSIX shells as well;
try it with ksh, bash, zsh, etc.

Unless you want to write widely portable (POSIX) scripts use the one
that serves best or that is available in your system environments.
And the powerful modern shells (ksh, bash, zsh) support a common base
of extended features; feel free to use them if you are not restricted
by other rules or requirements.

Janis

>
> #/bin/sh
>
> case $(( $$ %3)) in
>
> 0) echo "case 1" ;;
>
> 1) echo "case 2" ;;
>
> 2) echo "case 3" ;;
>
> esac
>

Re: random case construct in ksh

<5vd7si-gep.ln1@ID-313840.user.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5582&group=comp.unix.shell#5582

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!paganini.bofh.team!weretis.net!feeder8.news.weretis.net!lilly.ping.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geoff@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.shell
Subject: Re: random case construct in ksh
Date: Mon, 8 Aug 2022 13:59:50 +0100
Lines: 36
Message-ID: <5vd7si-gep.ln1@ID-313840.user.individual.net>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<tcqaim$1lt8$1@gioia.aioe.org>
Reply-To: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net oRgWSkTN9aTofTIiuFELywYp/H9Gw0y5425eDEevzd34m6mNfh
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:1wwSFpKbo1MOUK7bDNmpC7mwGVM=
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
 by: Geoff Clare - Mon, 8 Aug 2022 12:59 UTC

Laurens Kils-Hütten wrote:

> In /bin/sh this works:
>
> #/bin/sh
>
> case $(( $$ %3)) in
>
> 0) echo "case 1" ;;
>
> 1) echo "case 2" ;;
>
> 2) echo "case 3" ;;
>
> esac

In your original post you said you wanted to choose "at random".
Using $(($$%3)) is very much not random:

$ for i in {1..10}; do sh -c 'echo $(($$%3))'; done
1 2
0 1
2 0
1 2
0 1

(on almost all systems -- I believe there are some that randomise
PID allocation).

--
Geoff Clare <netnews@gclare.org.uk>

RANDOM numbers in shell (Was: random case construct in ksh)

<tcr8oi$3t66c$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5584&group=comp.unix.shell#5584

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: RANDOM numbers in shell (Was: random case construct in ksh)
Date: Mon, 8 Aug 2022 15:05:54 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tcr8oi$3t66c$1@news.xmission.com>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me> <tcqaim$1lt8$1@gioia.aioe.org> <5vd7si-gep.ln1@ID-313840.user.individual.net>
Injection-Date: Mon, 8 Aug 2022 15:05:54 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="4102348"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 8 Aug 2022 15:05 UTC

In article <5vd7si-gep.ln1@ID-313840.user.individual.net>,
Geoff Clare <netnews@gclare.org.uk> wrote:
....
>In your original post you said you wanted to choose "at random".
>Using $(($$%3)) is very much not random:

Leaving aside the philosophical question of whether any number generated by
software is truly random (i.e., anything short of attaching a Geiger
counter to your PC), using $$ as a sort-of-random-number in a shell script
is a fairly common, although flawed, practice.

Modern shells (e.g., bash) have RANDOM as a way of getting actual (subject,
of course, to the caveats mentioned above) random number in a shell script.
I don't know what other shells have RANDOM.

Anyway, I found the original thread topic confusing, so I changed it.

--
I'm building a wall.

Re: random case construct in ksh

<tcrh4h$1056v$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5585&group=comp.unix.shell#5585

  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: Mon, 8 Aug 2022 19:28:49 +0200
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <tcrh4h$1056v$1@dont-email.me>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<tcqaim$1lt8$1@gioia.aioe.org> <tcqoqq$tk1l$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 8 Aug 2022 17:28:49 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="45c825425490e9de6304db6e5da4286c";
logging-data="1053919"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+JQb3nVz5WI/4YsM16iiXI"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:cp0yjspH10ajxmW4p3XEdlIrsGQ=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <tcqoqq$tk1l$1@dont-email.me>
 by: Janis Papanagnou - Mon, 8 Aug 2022 17:28 UTC

On 08.08.2022 12:34, Janis Papanagnou wrote:
> On 08.08.2022 08:30, Laurens Kils-Hütten wrote:
>> Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
>>> The only thing you probably cannot rely on in POSIX shells is
>>> the RANDOM variable (which ksh of course supports).
>>
>> [...]
>>
>> In /bin/sh this works:
>
> I don't see why that shouldn't work in other POSIX shells as well;
> try it with ksh, bash, zsh, etc.
>
> Unless you want to write widely portable (POSIX) scripts use the one
> that serves best or that is available in your system environments.
> And the powerful modern shells (ksh, bash, zsh) support a common base
> of extended features; feel free to use them if you are not restricted
> by other rules or requirements.
>
> Janis
>
>>
>> #/bin/sh
>>
>> case $(( $$ %3)) in

I spoke about availability of RANDOM in modern powerful shells
(like ksh, bash, zsh) as opposed to the POSIX standard. Others
pointed out already about the non-randomness of $$.

In case you cannot or don't want to use one of these shells,
which are the simplest access to some randomness, there's also
the Unix operating system that _may_ support random data; check
whether you have /dev/urandom available to get random data.

Janis

Re: RANDOM numbers in shell (Was: random case construct in ksh)

<slrntf2g92.1b5v.naddy@lorvorc.mips.inka.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5586&group=comp.unix.shell#5586

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: naddy@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: RANDOM numbers in shell (Was: random case construct in ksh)
Date: Mon, 8 Aug 2022 17:00:18 -0000 (UTC)
Message-ID: <slrntf2g92.1b5v.naddy@lorvorc.mips.inka.de>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<tcqaim$1lt8$1@gioia.aioe.org>
<5vd7si-gep.ln1@ID-313840.user.individual.net>
<tcr8oi$3t66c$1@news.xmission.com>
Injection-Date: Mon, 8 Aug 2022 17:00:18 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="44224"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Mon, 8 Aug 2022 17:00 UTC

On 2022-08-08, Kenny McCormack <gazelle@shell.xmission.com> wrote:

> Modern shells (e.g., bash) have RANDOM as a way of getting actual (subject,
> of course, to the caveats mentioned above) random number in a shell script.
> I don't know what other shells have RANDOM.

Well, it's a ksh88 feature.

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: random case construct in ksh

<tcrim4$176d$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5587&group=comp.unix.shell#5587

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!sOAiplyedoQL/hL0V8UI7Q.user.46.165.242.75.POSTED!not-for-mail
From: lkh@sdf-eu.org (Laurens Kils-Hütten)
Newsgroups: comp.unix.shell
Subject: Re: random case construct in ksh
Date: Mon, 8 Aug 2022 17:55:16 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tcrim4$176d$1@gioia.aioe.org>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me> <tcqaim$1lt8$1@gioia.aioe.org> <tcqoqq$tk1l$1@dont-email.me> <tcrh4h$1056v$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="40141"; posting-host="sOAiplyedoQL/hL0V8UI7Q.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (NetBSD/9.1 (amd64))
X-Notice: Filtered by postfilter v. 0.9.2
 by: Laurens Kils-Hütten - Mon, 8 Aug 2022 17:55 UTC

Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
> On 08.08.2022 12:34, Janis Papanagnou wrote:
>> On 08.08.2022 08:30, Laurens Kils-Hütten wrote:
>>> Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
>>>> The only thing you probably cannot rely on in POSIX shells is
>>>> the RANDOM variable (which ksh of course supports).
>>>
>>> [...]
>>>
>>> In /bin/sh this works:
>>
>> I don't see why that shouldn't work in other POSIX shells as well;
>> try it with ksh, bash, zsh, etc.

$$ seems to be the only construct supported by netbsd's /bin/sh, so that's
what I'll use. Pseudo-randomness is enough for my use case.

>> Unless you want to write widely portable (POSIX) scripts use the one
>> that serves best or that is available in your system environments.

Exactly, the point is portability (and also to learn about the different
shells).

Thanks!

lkh

Re: RANDOM numbers in shell

<tcrj30$1clb$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5589&group=comp.unix.shell#5589

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!sOAiplyedoQL/hL0V8UI7Q.user.46.165.242.75.POSTED!not-for-mail
From: lkh@sdf-eu.org (Laurens Kils-Hütten)
Newsgroups: comp.unix.shell
Subject: Re: RANDOM numbers in shell
Date: Mon, 8 Aug 2022 18:02:08 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tcrj30$1clb$1@gioia.aioe.org>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me> <tcqaim$1lt8$1@gioia.aioe.org> <5vd7si-gep.ln1@ID-313840.user.individual.net> <tcr8oi$3t66c$1@news.xmission.com>
Injection-Info: gioia.aioe.org; logging-data="45739"; posting-host="sOAiplyedoQL/hL0V8UI7Q.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (NetBSD/9.1 (amd64))
X-Notice: Filtered by postfilter v. 0.9.2
 by: Laurens Kils-Hütten - Mon, 8 Aug 2022 18:02 UTC

Kenny McCormack <gazelle@shell.xmission.com> wrote:
> In article <5vd7si-gep.ln1@ID-313840.user.individual.net>,
> Geoff Clare <netnews@gclare.org.uk> wrote:
> ...
>>In your original post you said you wanted to choose "at random".
>>Using $(($$%3)) is very much not random:
>
> Leaving aside the philosophical question of whether any number generated by
> software is truly random (i.e., anything short of attaching a Geiger
> counter to your PC), using $$ as a sort-of-random-number in a shell script
> is a fairly common, although flawed, practice.
>
> Modern shells (e.g., bash) have RANDOM as a way of getting actual (subject,
> of course, to the caveats mentioned above) random number in a shell script.
> I don't know what other shells have RANDOM.

I see, turns out that netbsd's /bin/sh doesn't support RANDOM, yet. So $$
seems to be the only portable option. Sort-of-randomness is enough for
my use case.

>
> Anyway, I found the original thread topic confusing, so I changed it.
it reflected my state of mind, I guess ;D

--
Have this adventurer for your next game:
Peter dreams of riding a dragon ?
STR 12 INT 8 WIS 10 DEX 6 CON 11 CHA 11
HP 3 110 gp

Re: RANDOM numbers in shell

<tcrl5q$3tcdk$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5590&group=comp.unix.shell#5590

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: RANDOM numbers in shell
Date: Mon, 8 Aug 2022 18:37:46 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tcrl5q$3tcdk$1@news.xmission.com>
References: <tcm782$12hr$1@gioia.aioe.org> <5vd7si-gep.ln1@ID-313840.user.individual.net> <tcr8oi$3t66c$1@news.xmission.com> <tcrj30$1clb$1@gioia.aioe.org>
Injection-Date: Mon, 8 Aug 2022 18:37:46 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="4108724"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 8 Aug 2022 18:37 UTC

In article <tcrj30$1clb$1@gioia.aioe.org>,
Laurens Kils-Htten <lkh@sdf-eu.org> wrote:
>Kenny McCormack <gazelle@shell.xmission.com> wrote:
>> In article <5vd7si-gep.ln1@ID-313840.user.individual.net>,
>> Geoff Clare <netnews@gclare.org.uk> wrote:
>> ...
>>>In your original post you said you wanted to choose "at random".
>>>Using $(($$%3)) is very much not random:
>>
>> Leaving aside the philosophical question of whether any number generated by
>> software is truly random (i.e., anything short of attaching a Geiger
>> counter to your PC), using $$ as a sort-of-random-number in a shell script
>> is a fairly common, although flawed, practice.
>>
>> Modern shells (e.g., bash) have RANDOM as a way of getting actual (subject,
>> of course, to the caveats mentioned above) random number in a shell script.
>> I don't know what other shells have RANDOM.
>
>I see, turns out that netbsd's /bin/sh doesn't support RANDOM, yet.

Nor does "dash" on Debian systems.
Interestingly enough, busybox's sh does have RANDOM.

>So $$ seems to be the only portable option. Sort-of-randomness is enough
>for my use case.

I suppose if portabilty is the main concern, then using something like
/dev/urandom would be right out...

>> Anyway, I found the original thread topic confusing, so I changed it.
>
>it reflected my state of mind, I guess ;D

Quite so.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/TedCruz

Re: random case construct in ksh

<jlensnFe6foU1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5594&group=comp.unix.shell#5594

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.uzoreto.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: martijn@inlv.demon.nl (Martijn Dekker)
Newsgroups: comp.unix.shell
Subject: Re: random case construct in ksh
Date: Tue, 9 Aug 2022 10:32:07 +0100
Lines: 20
Message-ID: <jlensnFe6foU1@mid.individual.net>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net zXACGNLeZBT5p17JkZWu/gQ4mhOyERu4/xmRMf69RVg3VBiic=
Cancel-Lock: sha1:AY3zNzJT4njq9e6mWApId+ixWjc=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0)
Gecko/20100101 Thunderbird/102.1.0
Content-Language: en-GB
In-Reply-To: <tcmb08$nlm$1@dont-email.me>
 by: Martijn Dekker - Tue, 9 Aug 2022 09:32 UTC

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.

--
|| modernish -- harness the shell
|| https://github.com/modernish/modernish
||
|| KornShell lives!
|| https://github.com/ksh93/ksh

Re: random case construct in ksh

<tct9t1$1bq6k$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5595&group=comp.unix.shell#5595

  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: Tue, 9 Aug 2022 11:37:37 +0200
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <tct9t1$1bq6k$1@dont-email.me>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<jlensnFe6foU1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 9 Aug 2022 09:37:37 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="78d3505678a31bbc0f428f99f6c35a56";
logging-data="1435860"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19D5Vt/OMV3bfi6wE+ZtnRb"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:iAefPeoTp5pVkwk0jWxWyjoZ+18=
In-Reply-To: <jlensnFe6foU1@mid.individual.net>
 by: Janis Papanagnou - Tue, 9 Aug 2022 09:37 UTC

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.

Janis

Re: RANDOM numbers in shell

<c11asi-lf5.ln1@ID-313840.user.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5596&group=comp.unix.shell#5596

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!paganini.bofh.team!weretis.net!feeder8.news.weretis.net!lilly.ping.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geoff@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.shell
Subject: Re: RANDOM numbers in shell
Date: Tue, 9 Aug 2022 13:37:32 +0100
Lines: 18
Message-ID: <c11asi-lf5.ln1@ID-313840.user.individual.net>
References: <tcm782$12hr$1@gioia.aioe.org> <tcmb08$nlm$1@dont-email.me>
<tcqaim$1lt8$1@gioia.aioe.org>
<5vd7si-gep.ln1@ID-313840.user.individual.net>
<tcr8oi$3t66c$1@news.xmission.com> <tcrj30$1clb$1@gioia.aioe.org>
Reply-To: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net E+SyVRcmDxsckEBAgk2QaANG4gNKjy9wJWLanq+UjIZ+rHdm73
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:GlvDWdzVbVSJi0zR0e2AwhAiR98=
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
 by: Geoff Clare - Tue, 9 Aug 2022 12:37 UTC

Laurens Kils-Hütten wrote:

> I see, turns out that netbsd's /bin/sh doesn't support RANDOM, yet. So $$
> seems to be the only portable option. Sort-of-randomness is enough for
> my use case.

If you don't need a pure shell solution, you can do better by using
the rand() function in awk. If you seed it with $$, it will produce a
much more random looking sequence than $$ alone.

Something like:

case $(awk "BEGIN { srand($$); print int(3*rand()); }" /dev/null) in
....
esac

--
Geoff Clare <netnews@gclare.org.uk>

Re: random case construct in ksh

<ra0asi-lf5.ln1@ID-313840.user.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5597&group=comp.unix.shell#5597

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geoff@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.shell
Subject: Re: random case construct in ksh
Date: Tue, 9 Aug 2022 13:25:31 +0100
Lines: 34
Message-ID: <ra0asi-lf5.ln1@ID-313840.user.individual.net>
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: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net XVc/kn9HiRkEHvDnB25BgAydn0/AwVNpXz5z1cNliMe4ahLVJU
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:Y/fs+s59gFqAnGM8kDcOMqHn918=
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
 by: Geoff Clare - Tue, 9 Aug 2022 12:25 UTC

Janis Papanagnou wrote:

> On 09.08.2022 11:32, Martijn Dekker wrote:
>>
>> 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.

It was a common misconception in the early days, and the standard was
changed to clarify it at some point. However, _assigning_ to variables
was always clearly required to be supported, and the value of an
assignment operation is the new value of the assigned variable, so
it made no sense to interpret the standard as requiring:

echo $((x=42))

to output 42, but not requiring:

x=42; echo $((x))

to output 42.

--
Geoff Clare <netnews@gclare.org.uk>

Re: random case construct in ksh

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

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=5603&group=comp.unix.shell#5603

  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://www.rocksolidbbs.com/devel/article-flat.php?id=5607&group=comp.unix.shell#5607

  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://www.rocksolidbbs.com/devel/article-flat.php?id=5614&group=comp.unix.shell#5614

  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://www.rocksolidbbs.com/devel/article-flat.php?id=5615&group=comp.unix.shell#5615

  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