[SOLVED] 'RANDOM' not supported by POSIX?

Forum rules
Before you post please read how to get help
Post Reply
User avatar
Kyowash
Level 2
Level 2
Posts: 95
Joined: Mon Sep 25, 2017 1:22 pm
Location: /dev/full

[SOLVED] 'RANDOM' not supported by POSIX?

Post by Kyowash » Thu May 24, 2018 1:05 pm

I wanted to know what was like to write a portable script, and I'm having a bad time... I checked my script with shellcheck and I'm surprised because of this:

Code: Select all

In posix-test.sh line 15:
RandomNumber=$((RANDOM % 10))
                ^-- SC2039: In POSIX sh, RANDOM is not supported.
And instead I'm supposed to generate it with awk like this example:

Code: Select all

awk -v min=5 -v max=10 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'
And this surprised me because RANDOM is mentioned here.
So is it supported by POSIX or not?
Last edited by Kyowash on Sat May 26, 2018 10:43 am, edited 2 times in total.

User avatar
JoeFootball
Level 6
Level 6
Posts: 1393
Joined: Tue Nov 24, 2009 1:52 pm
Location: /home/usa/mn/minneapolis/joe

Re: 'RANDOM' not supported by POSIX?

Post by JoeFootball » Thu May 24, 2018 1:09 pm

Perhaps you're looking to use the $RANDOM variable? More information here.

Joe

rene
Level 7
Level 7
Posts: 1907
Joined: Sun Mar 27, 2016 6:58 pm

Re: 'RANDOM' not supported by POSIX?

Post by rene » Thu May 24, 2018 4:37 pm

Kyowash wrote:
Thu May 24, 2018 1:05 pm
And this surprised me because RANDOM is mentioned here.
I only see it mentioned in in section 8.1? There it's introduced by
It is unwise to conflict with certain variables that are frequently exported by widely used command interpreters and applications:
which is seemingly to say that indeed it's not standardised; the standard warns that you might want to avoid using a so named variable given that it's "frequently exported by widely used command interpreters". The following sections 8.2 and 8.3 -- and assumedly more sections scattered around the specifications -- list actually by the standard itself defined/mandated variables.

Other than that, yes, POSIX is very much least common denominator.

User avatar
Kyowash
Level 2
Level 2
Posts: 95
Joined: Mon Sep 25, 2017 1:22 pm
Location: /dev/full

Re: 'RANDOM' not supported by POSIX?

Post by Kyowash » Fri May 25, 2018 1:48 pm

JoeFootball wrote:
Thu May 24, 2018 1:09 pm
Perhaps you're looking to use the $RANDOM variable? More information here.

Joe
The problem is not that I used the RANDOM variable incorrectly. As you can see in the output of shellcheck, I used it right:

Code: Select all

RandomNumber=$((RANDOM % 10))
The problem is that if I ran that line with another shell like Sh it would not work because RANDOM is not supported by Sh.
rene wrote:
Thu May 24, 2018 4:37 pm
Kyowash wrote:
Thu May 24, 2018 1:05 pm
And this surprised me because RANDOM is mentioned here.
I only see it mentioned in in section 8.1? There it's introduced by
It is unwise to conflict with certain variables that are frequently exported by widely used command interpreters and applications:
which is seemingly to say that indeed it's not standardised; the standard warns that you might want to avoid using a so named variable given that it's "frequently exported by widely used command interpreters". The following sections 8.2 and 8.3 -- and assumedly more sections scattered around the specifications -- list actually by the standard itself defined/mandated variables.
Yeah, that line misled me so that I thought that list was formed by standard environment variables. My bad!
So the variables supported by POSIX are just those found in section 8.2 and 8.3? That's a quite reduced list compared to the one I thought that was the correct one. Not even USER is found there. And I'm again surprised because I made another script called check-root-posix.sh with this content:

Code: Select all

#!/bin/bash
if [ "$USER" = "root" ]
    then
        echo "User is root."
fi
And shellcheck -s sh check-root-posix.sh didn't complaint about it. I'm so confused right now.

lmuserx4849
Level 5
Level 5
Posts: 800
Joined: Wed Dec 17, 2014 2:55 am

Re: 'RANDOM' not supported by POSIX?

Post by lmuserx4849 » Fri May 25, 2018 1:49 pm

Great question.

If the first line is /bin/sh, bash runs in posix mode.

Random is mentioned as an API, but not a Utility or Shell command.

From a posix shell script, there doesn't seem to be a way to get a random number without calling an external application.

Code: Select all

tr -d -c  '[:digit:]'  < /dev/urandom | head -c 4 
Besides awk and tr, maybe Utilities: od or dd.

But to be nice, you'd have to check that each utility exists before actually using it. There are no guarantees. Even awk could be gawk or versions could differ.

User avatar
Kyowash
Level 2
Level 2
Posts: 95
Joined: Mon Sep 25, 2017 1:22 pm
Location: /dev/full

Re: 'RANDOM' not supported by POSIX?

Post by Kyowash » Fri May 25, 2018 2:00 pm

lmuserx4849 wrote:
Fri May 25, 2018 1:49 pm
If the first line is /bin/sh, bash runs in posix mode.
I may be wrong, but if the first line (known as Shebang) is #!/bin/sh it doesn't mean that Bash runs in POSIX mode, it means the script will be run by Sh itself. Or rather, it will be run by the Debian Almquist Shell:

Code: Select all

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Jan 19  2017 /bin/sh -> dash
To set Bash in POSIX mode you can use the --posix option or use set:

Code: Select all

set -o posix
lmuserx4849 wrote:
Fri May 25, 2018 1:49 pm
There are no guarantees. Even awk could be gawk or versions could differ.
I wish I hadn't proposed myself to learn how to write portable shell scripts. :mrgreen:

rene
Level 7
Level 7
Posts: 1907
Joined: Sun Mar 27, 2016 6:58 pm

Re: 'RANDOM' not supported by POSIX?

Post by rene » Fri May 25, 2018 5:51 pm

Kyowash wrote:
Fri May 25, 2018 1:48 pm
So the variables supported by POSIX are just those found in section 8.2 and 8.3?
Sort of doubt it (hence the "assumedly [ ... ]" above) but can't say that I know. Frankly I tend to treat any environment variable as non-guaranteed; use e.g. $(whoami) in lieu of $USER, ~/ for $HOME and so on. Now, I also expect that in for example that latter case ~/ might not in fact be POSIX whereas $HOME is -- but I sort of refuse to check. I do tend to try to avoid (or at least be aware) of bashisms even if only since I'm not overly fond of bash but other than that I feel it's not all that useful to limit myself to POSIX when realistically speaking any shell code I'd write only ever runs on Linux systems.

User avatar
Kyowash
Level 2
Level 2
Posts: 95
Joined: Mon Sep 25, 2017 1:22 pm
Location: /dev/full

Re: 'RANDOM' not supported by POSIX?

Post by Kyowash » Sat May 26, 2018 10:43 am

rene wrote:
Fri May 25, 2018 5:51 pm
Kyowash wrote:
Fri May 25, 2018 1:48 pm
So the variables supported by POSIX are just those found in section 8.2 and 8.3?
Sort of doubt it (hence the "assumedly [ ... ]" above) but can't say that I know. Frankly I tend to treat any environment variable as non-guaranteed; use e.g. $(whoami) in lieu of $USER, ~/ for $HOME and so on. Now, I also expect that in for example that latter case ~/ might not in fact be POSIX whereas $HOME is -- but I sort of refuse to check. I do tend to try to avoid (or at least be aware) of bashisms even if only since I'm not overly fond of bash but other than that I feel it's not all that useful to limit myself to POSIX when realistically speaking any shell code I'd write only ever runs on Linux systems.
I feel the same way right now. To be honest I doubt I'll ever have to make a script on another *nix, I was just wondering what was it like to write portable shell scripts and it's giving me a headache for a simple script. It looks like apart from awk I can use /dev/random or /dev/urandom. I don't want to know what it will be like to make a POSIX equivalent of an array with a for loop.

Thank you all for your help.

lmuserx4849
Level 5
Level 5
Posts: 800
Joined: Wed Dec 17, 2014 2:55 am

Re: 'RANDOM' not supported by POSIX?

Post by lmuserx4849 » Mon May 28, 2018 4:18 pm

https://www.gnu.org/software/bash/manua ... POSIX-Mode
Starting Bash with the --posix command-line option or executing ‘set -o posix’ while Bash is running will cause Bash to conform more closely to the POSIX standard by changing the behavior to match that specified by POSIX in areas where the Bash default differs.

When invoked as sh, Bash enters POSIX mode after reading the startup files.

The following list is what’s changed when ‘POSIX mode’ is in effect:

1. When a command in the hash table no longer exists, Bash will re-search $PATH to find the new location. This is also available with ‘shopt -s checkhash’.
...
56. ...

rene
Level 7
Level 7
Posts: 1907
Joined: Sun Mar 27, 2016 6:58 pm

Re: 'RANDOM' not supported by POSIX?

Post by rene » Tue May 29, 2018 6:14 am

lmuserx4849 wrote:
Mon May 28, 2018 4:18 pm
When invoked as sh, Bash enters POSIX mode after reading the startup files.
Sure, but the thing is that on Debian/Ubuntu/Mint /bin/sh does not invoke bash but dash:

Code: Select all

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 feb 20 10:54 /bin/sh -> dash

Post Reply

Return to “Scripts & Bash”