Article View: linux.debian.user
Article #263Re: Bash: check if a value is into an array
From: Jeffrey Walton
Date: Thu, 22 Oct 2026 17:40
Date: Thu, 22 Oct 2026 17:40
50 lines
5539 bytes
5539 bytes
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?
Jeff
Technical Details
Message-ID:
<NR4VH-7e8T-7@gated-at.bofh.it>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.corradoroberto.it!news.servidellagleba.it!bofh.it!news.nic.it!robomodReferences:
<NQFND-6ZMU-9@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!news.corradoroberto.it!news.servidellagleba.it!bofh.it!news.nic.it!robomod
From: Jeffrey Walton <noloader@gmail.com>
Newsgroups: linux.debian.user
Subject: Re: Bash: check if a value is into an array
Date: Thu, 22 Oct 2026 19:40:01 +0200
Message-ID: <NR4VH-7e8T-7@gated-at.bofh.it>
References: <NQFND-6ZMU-9@gated-at.bofh.it>
X-Mailbox-Line: From debian-user-request@lists.debian.org Thu Oct 22 17:38:50 2026
Old-Return-Path: <noloader@gmail.com>
X-Amavis-Spam-Status: No, score=-2.099 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_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001]
autolearn=ham autolearn_force=no
X-Policyd-Weight: using cached result; rate: -5.5
Arc-Seal: i=1; a=rsa-sha256; t=1792690710; cv=none;
d=google.com; s=arc-20260327;
b=Na1aK4gql+6xgCf7RZAmverI4JLeAyLdGBD/y9JNxyYwKoK+82Cts+A/W66Xg8titc
X15QosZsEqEGfpslQKX7LlD0YZLRWYMBCNy98pntvtD1/BY8oiWdstHD8vfpI6KiRVTf
Uco7SAPB+zp5N14LxBkWOA/tao6tPM4tvz775KH6g22hkll+gxpXUzLAqGi/s8sm93im
kEJp2czCWIkjX7b7Gm3wuwkS/1PS6JYCVh+Cbvxaiunefpe9HLJ/75KeFOD4xX4qIpp3
0usol/r9smLQxh4UHvqR1kexp4PMWcyoJGtEYZCu2T9EbWzawYOnbLzRpizGl/iTXvz8
/+cw==
Arc-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327;
h=content-transfer-encoding:cc:to:subject:message-id:date:from
:reply-to:in-reply-to:references:mime-version:dkim-signature;
bh=P/iY0GM/0bFwP+k9R8nr5w9zDL8CovlpfJBBtIYvbfw=;
fh=IP8C83bvUuekVqXFwTe9yB/472MqgZQ14cYOMCnk4HI=;
b=ItjVX2JFdgaN3yxpoo8QGUOG+hmX6PMoV1UP5uYBh+7/RjIiT9LT5FpHfqQdfAAprS
YJf9Fw8zgP01uNMhHqZGbLipkv5MxWrPZrwtl81xs73ftpdG/piR7rkCNzQfmWn41o9S
cBCkE98G6W+/8OwW2CS4JObZgtukRDJ/S16DOWqIoW8AUnnnqyak3uSANzYOkOq+lU9X
2gji2GhYMCCNIMFmfrFo8F82vfElC+atNbkPNRpMKQCitgIvFemhij+zkPZdcRG+1HjD
/TBCzGtU3McOZ6Qy6R+ax4q06ec3aMi6EBD0+d66mDZOaGYXE1u+SN2rh0tDtznFWcV8
0G9g==;
darn=lists.debian.org
Arc-Authentication-Results: i=1; mx.google.com; arc=none
X-Gm-Message-State: AFq9FYKROpGN5gAuSxRL+aYmOtCyCvfFFU9A2vX6gEQyheTk+YB7tcS/
C3o0y7JXIBl1ZzCOo18RSmo9iarOdwTE/7ZbL7m8F0COpYiSb/gr+KO6NVPaZ99m7/DclN7LCSp
QdFb0MnTqqYE7OCj0XbQrd6u5BJud9Nq+ROr9
X-Gm-Gg: AYBFou2kyhLHDEzSIU63qCBCS0bB9Ooo/aJfOz9o1PchSC/9eG182KnxSosP5fBFQ0z
4cL75egXksIK80Yg8kkTQa2WRmEfbCzwKCwwKET8aUUDjRE8qTBiC7thU6qXlx77N2cRfsZfvn2
r+I8TcPA0eqDV0Qm7Qkku7/kC/ed3/tFM4kyz2FRdXmn+Cwuu8jvU27oKnrAJxCLLSr+HR4Mup7
DA+qXA73DzuMY0xvaPftZg/73zNTxginNM0SRsIzNIyH0WwL9QmZ5Rq1IGlx5w4WmIF8TykpPLi
pymqBjJO7SvQkhc60G9uG5mX8Uky9Iov0Hsuk6p+U2R0XidheUpxEPcK/aAhzWAn3sv57+rUPAC
kZEZvxHeqmxg9ktY3cWmAVe0weBfQTTgud3sEplfwdqIA755nkbIUysT7S3UfBSURBVzizh5/fU
LLOoNiX7yS9Js1UkaUWQTbT3ipy57cL25ZE0gsLQQ+m6QDXnDLM+nlkhvEEuBzN+e00Eu4cNZIY
5c7oZB4/GjhnUbe/FvbKz9UkJrWJyoE3Sjt7yNG
X-Received: by 2002:a05:651c:1509:b0:3ab:fecf:51f5 with SMTP id
38308e7fff4ca-3ac44179476mr16819601fa.14.1792690709829; Thu, 22 Oct 2026
10:38:29 -0700 (PDT)
MIME-Version: 1.0
Reply-To: noloader@gmail.com
X-Gm-Features: AclHuK_VQXqPOFCLukumOEy4xvMG3URQLiOvx6-gYPpvuW7NXYz7RlVp-jIbdOY
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Mailing-List: <debian-user@lists.debian.org> archive/latest/832575
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/CAH8yC8=snY2=+6wjF_h7mFy87yfjqEXgp7AnjxBOYt-dgTdwAg@mail.gmail.com
Approved: robomod@news.nic.it
Lines: 50
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 13:37:52 -0400
X-Original-Message-ID: <CAH8yC8=snY2=+6wjF_h7mFy87yfjqEXgp7AnjxBOYt-dgTdwAg@mail.gmail.com>
X-Original-References: <fb4588c5-2637-4714-81e6-791a9260afbd@gmail.com>