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
The only thing you probably cannot rely on in POSIX shells is
the RANDOM variable (which ksh of course supports).
Janis Papanagnou <[email protected]> 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
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:
On 08.08.2022 12:34, Janis Papanagnou wrote:
On 08.08.2022 08:30, Laurens Kils-H�tten wrote:
Janis Papanagnou <[email protected]> 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.
On 08.08.2022 08:30, Laurens Kils-H�tten wrote:
Janis Papanagnou <[email protected]> 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
In article <[email protected]>,
Geoff Clare <[email protected]> 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.
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.
Kenny McCormack <[email protected]> wrote:
In article <[email protected]>,
Geoff Clare <[email protected]> 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
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.
For readability I prefer the [non-standard] $(( RANDOM % 3 ))
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.
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.
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 Papanagnou <[email protected]>:
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}))"
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 05:08:42 |
| Calls: | 12,100 |
| Calls today: | 8 |
| Files: | 15,003 |
| Messages: | 6,517,902 |
| Posted today: | 1 |