๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.debian.user
Article #218

Bash: check if a value is into an array

#218
From: Franco Martelli
Date: Wed, 21 Oct 2026 14:50
34 lines
4570 bytes
Hi,

I run into issue trying to populate an array with unique random value, 
the issue is that I have an endless loop, here the snippet:

...
# Populate playlist array with unique random values
while [ "$index" -le "$total" ]; do
         clip="$(/usr/bin/numrandom /1..$total/)"
         if [[ ! ${playlist[@]} =~ "$clip" ]]; then      # [ -z 
${rndarr[clip]} ]; then
                                                         # rndarr[$clip]=1
                playlist[$index]=$clip
                ((index++))
         fi
done
...

It works fine if I use the second array "rndarr[]". I use that array to 
store 1 at the rndarr[$index] occurrence then, after incremented the 
index, I check whether it's empty (no 1 stored) at the new $index value.

My wishes is to avoid to use a second array (rndarr[]) and use 
playlist[] array only to do this, but with the above code I have an 
endless loop, why?

I checked the remarkable ABS ยน handbook at chapter 27 but nothing helped 
about finding values inside an array (only replacement examples).

Thanks in advance for any hint, kind regards,

ยน https://tldp.org/LDP/abs/abs-guide.pdf
-- 
Franco Martelli
Technical Details
Message-ID: <NQFND-6ZMU-9@gated-at.bofh.it>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!3.eu.feeder.erje.net!feeder.erje.net!usenet.goja.nl.eu.org!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomod

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!3.eu.feeder.erje.net!feeder.erje.net!usenet.goja.nl.eu.org!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomod
From: Franco Martelli <martellif67@gmail.com>
Newsgroups: linux.debian.user
Subject: Bash: check if a value is into an array
Date: Wed, 21 Oct 2026 16:50:01 +0200
Message-ID: <NQFND-6ZMU-9@gated-at.bofh.it>
X-Mailbox-Line: From debian-user-request@lists.debian.org  Wed Oct 21 14:45:47 2026
Old-Return-Path: <martellif67@gmail.com>
X-Amavis-Spam-Status: No, score=-1.849 tagged_above=-10000 required=5.3
 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
 DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001] autolearn=ham autolearn_force=no
X-Policyd-Weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 CL_IP_EQ_HELO_IP=-2 (check from: .gmail. - helo: .mail-wm1-x32b.google. - helo-domain: .google.)  FROM/MX_MATCHES_HELO(DOMAIN)=-2; rate: -5.5
X-Gm-Message-State: AFq9FYJfTFZiZ/7D89jJ1GPHQ3EHH8QPkOdM/NprGNHykWj23gOPoCmJ
	QTiOrNWu6y1lM/+FtEibIMc7yNR+HX8+R/p4hDOYTqnEyRtrwWsQZsHI3J6jRA==
X-Gm-Gg: AYBFou1ae8PPCD1F80bfU8rR4RvmyPbSgBfxOmPKgBuhuE9/E6yQytju7tknRd6q80v
	m9Oomn/s1kWJP2BQwQh87Seozwg6C+o2FLZGZCB2xk9gkunokn4KOOhf+loejnxXxzJ/TtyGRNK
	qepk7cp54+Pai+7IFPxd4dTP88UNOR1xng0fF3EqIjqQ+LIOAP9b7Is/Svzd/WDxp3wVju2FSd0
	K0NjEW50fL3MlB7ks0VTmGBdEXHd75DMTMgK19PQ3o2sNUqWwnfF2eKKkfmqiBznpD4+Fmf0aYd
	1K9I+K27H4j9SpJk5XmP5pETwp85EEOcwVFyi2yQ66wxqFze5M5YfrI7lIXLK4cIFgYGhszP+Ud
	+yHJp1A6FxK/yKF+WhJJhBiJqO2PJds26Q4wAGAmwcrr7aP8yQ/1fnliTa9Gc8cCjhjCnoaiBr2
	psTmSWfbhxzfDJ/o5LoJ+1vLGWoPV3w/T5D2YG3UcpzfcHCai2/Bq3/weMEz9ZeGLjGk3vr7ZcK
	y4V9O8mT2RyyVcmDrvTdo/InEVFBxFaOFQ8GQG6VDp6S7lGw6K+5HZw+ta92zn+t5bZeldb9QTN
	bjcppFKl2D1a
X-Received: by 2002:a05:600c:a41:b0:4a1:df52:3e36 with SMTP id 5b1f17b1804b1-4a1fdd1038emr51301415e9.27.1792593933499;
        Wed, 21 Oct 2026 07:45:33 -0700 (PDT)
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Content-Language: en-US
Autocrypt: addr=martellif67@gmail.com; keydata=
 xjMEXwx9ehYJKwYBBAHaRw8BAQdA8CkGKYFI/MK9U3RPhzE5H/ul7B6bHu/4BIhTf6LLO47N
 J0ZyYW5jbyBNYXJ0ZWxsaSA8bWFydGVsbGlmNjdAZ21haWwuY29tPsKWBBMWCAA+FiEET9sW
 9yyU4uM6QbloXEn0O0LcklAFAmhyroACGwMFCQ0ncuYFCwkIBwIGFQoJCAsCBBYCAwECHgEC
 F4AACgkQXEn0O0LcklAHVwD9H5JZ52g292FD8w0x6meDD8y/6KkNpzuaLHP6/Oo8kAIBAJsh
 aMB9LdCBJTMtnxU8JTHtAoGOZ/59UJWeZIkuWJUNzjgEXwx9ehIKKwYBBAGXVQEFAQEHQNP5
 V2q0H0oiJu89h1SSPgQDtkixXvUvRf1rNLLIcNpPAwEIB8J+BBgWCAAmFiEET9sW9yyU4uM6
 QbloXEn0O0LcklAFAmhyroACGwwFCQ0ncuYACgkQXEn0O0LcklASVwEAoEkHMEU7mHc0zmAu
 D2R1PYsDh9+3wQeied5PrF+HdakBAOeSGsf40GBew5umZuM59I04d1uXYAXGMP+jGN2RUtMA
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Mailing-List: <debian-user@lists.debian.org> archive/latest/832524
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/fb4588c5-2637-4714-81e6-791a9260afbd@gmail.com
Approved: robomod@news.nic.it
Lines: 34
Organization: linux.* mail to news gateway
Sender: robomod@news.nic.it
X-Original-Date: Wed, 21 Oct 2026 16:45:30 +0200
X-Original-Message-ID: <fb4588c5-2637-4714-81e6-791a9260afbd@gmail.com>