Article View: linux.debian.user
Article #265Re: Bash: check if a value is into an array
From: Franco Martelli
Date: Thu, 22 Oct 2026 18:20
Date: Thu, 22 Oct 2026 18:20
54 lines
5253 bytes
5253 bytes
On 22/10/26 at 19:37, Jeffrey Walton wrote:
> On Wed, Oct 21, 2026 at 12:34 PM Franco Martelli <martellif67@gmail.com> wrote:
>>
>> 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
>> ...
>
> The code is broken:
>
> $ shellcheck test.sh
>
> In test.sh line 2:
> while [ "$index" -le "$total" ]; do
> ^-- SC1009 (info): The mentioned syntax error was in this while loop.
>
> In test.sh line 4:
> if [[ ! ${playlist[@]} =~ "$clip" ]]; then # [ -z
> ^-- SC1046 (error): Couldn't find 'fi' for this 'if'.
> ^-- SC1073 (error): Couldn't parse this if expression. Fix to allow mor
> e checks.
>
>
> In test.sh line 5:
> ${rndarr[clip]} ]; then
> ^-- SC1047 (error): Expected 'fi' matching previously mention
> ed 'if'.
> ^-- SC1072 (error): Unexpected keyword/token. Fix any men
> tioned problems and try again.
>
> Perhaps you can supply a working example?
already clarified, maybe a matching issue in the "if [[ ! ${playlist[@]}
=~ "$clip" ]]; then" statement, please see "Second note:" in the
following email:
https://lists.debian.org/debian-user/2026/10/msg00376.html
Kind regards,
--
Franco Martelli
Technical Details
Message-ID:
<NR5yp-7exf-5@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!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomodReferences:
<NQFND-6ZMU-9@gated-at.bofh.it> <NR4VH-7e8T-7@gated-at.bofh.it>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!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomod
From: Franco Martelli <martellif67@gmail.com>
Newsgroups: linux.debian.user
Subject: Re: Bash: check if a value is into an array
Date: Thu, 22 Oct 2026 20:20:01 +0200
Message-ID: <NR5yp-7exf-5@gated-at.bofh.it>
References: <NQFND-6ZMU-9@gated-at.bofh.it> <NR4VH-7e8T-7@gated-at.bofh.it>
X-Mailbox-Line: From debian-user-request@lists.debian.org Thu Oct 22 18:17:13 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: using cached result; rate: -5.5
X-Gm-Message-State: AFq9FYL5SfJ62aFlOHfEPgP7FDXuWZps8phORG4QDv3Sx1KFKyNvlfBb
nKt473xq8YDfe8g10UUhsW2r67rpBW+MWYv3W/OUgzmyWqpnBJVOM+uG
X-Gm-Gg: AYBFou3zmA+ZIRuhw3RXduSnUYJtbDXcnZyJYkNn/30Yqd0x80MDGcW7iOfru9Koxd1
BJ5b4OZJoRM4Fp8WnjipT58uxg2K6BAjd39yDrM4oplPBiIuDTkJhE2cCnp5sp/nwy8euAAE0y6
SPjtjPWRfYmeqQZjk/+oaxPCXE6heUgL6kgP6aDwaF30xRBiFkf4LnMI1mEvgiugvAk6Pp4SOxs
ZKklW/JljcJzETptsQWmbtBLCYMiSrf8oYk0Nc45h+89tl8rBGbE4s0yZHeVmbuIp9bTv3gTcs0
pB0NO2KjtWJiWL975DoRZ8SxSGtdHmq1zgqtvJOZw/tNSIZMJfZb0IE8frKIGwbLz3QvMniwZHF
2tn16xuNG2m4S+LLs67fBt4UZowwBoylhuhzrCqxBlaPtx3kKuiK3cSM1gsdxOQQD2FuukP6YJc
sHFtT4Bfxy/PlCEtEGE+ZCZ7B6HtLv+AhfsB/cH9QD9XZSnpmyv/TmIJDS8fsYh8FCXGW8F9OLF
RjtmMT2mvhL6aaY4HAxx+sc3QONSl2ZN1W5PvGRa62njmkt4SAIW5MShWlMeHPoCfsYH+D96HDj
jx8FDmdc2JYQ
X-Received: by 2002:a05:600c:8714:b0:4a1:d97e:cd60 with SMTP id 5b1f17b1804b1-4a1fdd1604fmr128790785e9.16.1792693018321;
Thu, 22 Oct 2026 11:16:58 -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/832577
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/c0ce2bfe-c761-4376-98d9-f00a1ddf8ddd@gmail.com
Approved: robomod@news.nic.it
Lines: 54
Organization: linux.* mail to news gateway
Sender: robomod@news.nic.it
X-Original-Cc: debian-user@lists.debian.org
X-Original-Date: Thu, 22 Oct 2026 20:16:56 +0200
X-Original-Message-ID: <c0ce2bfe-c761-4376-98d9-f00a1ddf8ddd@gmail.com>
X-Original-References: <fb4588c5-2637-4714-81e6-791a9260afbd@gmail.com>
<CAH8yC8=snY2=+6wjF_h7mFy87yfjqEXgp7AnjxBOYt-dgTdwAg@mail.gmail.com>