Article View: linux.debian.user
Article #278Re: Bash: check if a value is into an array
From: Franco Martelli
Date: Fri, 23 Oct 2026 15:30
Date: Fri, 23 Oct 2026 15:30
77 lines
6173 bytes
6173 bytes
On 22/10/26 at 16:22, Greg Wooledge wrote:
> For this project, it would be better to shuffle the array of files
> instead of selecting one at a time.
>
> #!/bin/bash
>
> # Gather the files.
> cd ~/Video/clips/partyzone/ || exit
> playlist=([0-9][0-9][0-9][0-9]*)
>
> # Shuffle the playlist.
> n=${#playlist[@]}
> for ((i=n-1; i>0; i--)); do
> max=$(( 32768 / (i+1) * (i+1) ))
> while (( (rand=RANDOM) >= max )); do :; done
> rand=$(( rand % (i+1) ))
> tmp=${playlist[i]} playlist[i]=${playlist[rand]} playlist[rand]=$tmp
> done
>
> # Play the shuffled playlist.
> for clip in "${playlist[@]}"; do
> ffplay -vf "scale=720:576,setdar=720/576" -autoexit -volume 10 \
> "$clip" >/dev/null 2>&1
> done
>
> You can add your trap if you want it.
Greg, there is something odd in the random number generator, when you
have some spare time could you take a look at the code, please:
...
#!/bin/bash
# Gather the files.
cd ~/Video/clips/partyzone/ || exit
playlist=([0-9][0-9][0-9][0-9]*)
declare -a assigned # Declare an indexed array of name: assigned
# set -x
# Shuffle the playlist.
n=${#playlist[@]}
for ((i=n; i>0; )); do
max=$(( 32768 / (i+1) * (i+1) ))
while (( (rand=RANDOM) >= max )); do :; done
rand=$(( rand % (i+1) ))
rand=$((RANDOM % n))
if [ -z "${assigned[rand]}" ]; then
assigned[$rand]=1
((i--))
else
continue
fi
tmp=${playlist[i]} playlist[i]=${playlist[rand]} playlist[rand]=$tmp
echo $rand
done
exit
# Play the shuffled playlist.
for clip in "${playlist[@]}"; do
ffplay -vf "scale=720:576,setdar=720/576" -autoexit -volume 10 "$clip"
>/dev/null 2>&1
done
I've added the "if [ -z "${assigned[rand]}" ]; then" statement because
$rand had many occurrences of the same value so I tried with the
"assigned[$rand]" array to avoid that, looping in the circumstance
"$rand" had a value already assigned.
The issue is that when I comment out the line "rand=$((RANDOM % n))" and
place ((i=n-1; i>0; )) in the "for" statement header, the script runs in
an endless loop. :(
I don't know why, I tried to enable debug with "set -x" but for me
doesn't help, it looks like that the range of generated value is too
small to complete "i--" iterations, maybe I'm wrong.
Thanks in advance, kind regards,
--
Franco Martelli
Technical Details
Message-ID:
<NRpnr-7pXh-11@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!weretis.net!feeder8.news.weretis.net!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomodReferences:
<NQFND-6ZMU-9@gated-at.bofh.it> <NQG6Z-703J-7@gated-at.bofh.it> <NQJod-71PP-13@gated-at.bofh.it> <NQJRf-729Y-1@gated-at.bofh.it> <NR1br-7bQc-11@gated-at.bofh.it> <NR1XP-7chD-5@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!weretis.net!feeder8.news.weretis.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: Fri, 23 Oct 2026 17:30:01 +0200
Message-ID: <NRpnr-7pXh-11@gated-at.bofh.it>
References: <NQFND-6ZMU-9@gated-at.bofh.it> <NQG6Z-703J-7@gated-at.bofh.it> <NQJod-71PP-13@gated-at.bofh.it> <NQJRf-729Y-1@gated-at.bofh.it> <NR1br-7bQc-11@gated-at.bofh.it> <NR1XP-7chD-5@gated-at.bofh.it>
X-Mailbox-Line: From debian-user-request@lists.debian.org Fri Oct 23 15:26:22 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: AFq9FYJLvRD2YzlgnkgrU+BsSKgBoziB/6uxs6w5cAE11CwOaDPus+4A
782ujFyoKsRigYUkVqctOW+y66oza+P/f2uCuxQxx9C0pl5FP4Hes5xgsbJoAQ==
X-Gm-Gg: AYBFou0RIo2nzUcwndd0ocsEfCygtu6cJiNUjkMfvi/jIId03V9kC3EMmPiqC5QXX90
FfAQWSmv1J+MdehpUebZWEdAWRQ3HlKndjtIUrq2AeQQPRKwzZZpB2UgH2yKgUUenS7Nud74TIN
a45KWKwMWEWvxAoTCOhLry4Nl8IEvpOmiOZ/EfJwDk4lv4jwnTtYWR8gADvWpLkXjSXJczjgjSI
HMEtfIwJ6LCz0cPKhVlQYLfaEly9EJoh3UG2ZKcA5bbSvZ2Y5nNsj/m7DdCexC+YHCg/fJUfPdh
q8SiDeqBVwAXk0Od26zpegXQitkYtq/kaGWtlSXXQRFJbuBDXBGll9iOJYVWrGVbner4wPmPIq+
Sw+Kb+jVUsnuylfMbFyfD0FD+klqx53fs9p/cn0AvtOL+x2B4mUQ5GJ2m2HF6Hfg7r7aeOE+gMg
85qmO1dyONGugwg9dEzbkxR8GoSPUxMKOtLsxULOGPKG4HKVwQBIYHHLeRn/CmKi3Gj+wu+FScz
l71UzAEzXJUogD9UjPxzdAehardaXO29Mbr8QykZNP06iaqtLDRsgHuDRX9X7TxDiRAN2VFwFUE
pmROfYhLY3u9
X-Received: by 2002:a05:6000:2906:b0:48e:e2a:40e1 with SMTP id ffacd0b85a97d-48e385a2bcfmr4394717f8f.15.1792769164383;
Fri, 23 Oct 2026 08:26:04 -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: 7bit
X-Mailing-List: <debian-user@lists.debian.org> archive/latest/832590
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/4ca939f7-efa0-410a-a6a4-bf9d08bc14f4@gmail.com
Approved: robomod@news.nic.it
Lines: 77
Organization: linux.* mail to news gateway
Sender: robomod@news.nic.it
X-Original-Cc: debian-user@lists.debian.org
X-Original-Date: Fri, 23 Oct 2026 17:26:02 +0200
X-Original-Message-ID: <4ca939f7-efa0-410a-a6a4-bf9d08bc14f4@gmail.com>
X-Original-References: <fb4588c5-2637-4714-81e6-791a9260afbd@gmail.com>
<20261021150743.GA21484@wooledge.org>
<b397ac30-dbff-4822-b314-c6cc97dc0df8@gmail.com>
<20261021190429.GA28455@wooledge.org>
<02f8c2b4-80ec-4f0a-a277-3f1c80acd476@gmail.com>
<20261022142207.GE28455@wooledge.org>