commit: 43266e0ea45a6f9349e496ede4a8c6deaa9a611f
Author: Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Wed Oct 30 12:02:38 2024 +0000
Commit: Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Wed Oct 30 12:02:38 2024 +0000
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=43266e0e
Simplify the detection of SRANDOM in bash
Though undocumented, bash is programmed to discard random numbers that
are equal to whichever one was last generated. Hence, it is needless to
ever compare two consecutive expansions of SRANDOM more than once.
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
functions.sh | 20 +++++---------------
1 file changed, 5 insertions(+), 15 deletions(-)
diff --git a/functions.sh b/functions.sh
index 2414612..ec0df2d 100644
--- a/functions.sh
+++ b/functions.sh
@@ -586,21 +586,12 @@ quote_args()
#
srandom()
{
+ # The SRANDOM variable was introduced by bash 5.1. Check for at least
+ # 5.0 before comparing two expansions thereof. Doing so is safe because
+ # bash discards numbers that are equal to whicever was last generated.
+ #
# shellcheck disable=3028
- _has_srandom()
- {
- # The SRANDOM variable was introduced by bash 5.1. Check for at
- # least 5.0, letting the alternate branch confirm its efficacy.
- if [ "${BASH_VERSINFO-0}" -lt 5 ]; then
- false
- else
- for _ in 1 2 3; do
- test "${SRANDOM}" != "${SRANDOM}" && break
- done
- fi
- }
-
- if _has_srandom; then
+ if [ "${BASH_VERSINFO-0}" -ge 5 ] && [ "${SRANDOM}" != "${SRANDOM}" ];
then
srandom()
{
printf '%d\n' "$(( SRANDOM >> 1 ))"
@@ -650,7 +641,6 @@ srandom()
return 1
fi
- unset -f _has_srandom
srandom
}