A script to get a random file Dear developers,
I have an issue with my script switching from Android 9 to 10 (devices from a Umidigi S3 Pro to a Umidigi F2).
I have installed Bosybox App on the first and Busybox Magisk module on the latter.
Now the script does not work because the command:
list=(`busybox find &quot;$dirs&quot; -type f -name *.$ext`)

returns an empty array.
This is the complete script:
#!/system/bin/sh
echo
if test &quot;$1&quot; = &quot;&quot;; then
     echo &quot;Randomfile script by Uranya &lt;@uranya7x&gt; v1.4 01.01.2021&quot;
     echo &quot;Usage:&quot;
     echo &quot;sh randomfile.sh &lt;sourcedir&gt; &lt;extension&gt; &lt;destdir&gt;&quot;
     exit 1
fi
dirs=$1
ext=$2
dird=$3'/'
dest=$dird'random'
delim1=&quot;&quot;
delim2=&quot;&quot;
last='last.txt'
# create filename's array
IFS=$'\n'

list=(`busybox find &quot;$dirs&quot; -type f -name *.$ext`)

# count number of files
num=${#list[@]}
# generate random number in range 1-NUM
let &quot;ran=(${RANDOM} % ${num})+ 1&quot;
echo Random from $num files is $ran
sour=${list[ran]}
sourn=${sour#$dirs}
sourn=${sourn:1:${#sourn}}
date=$(date +&quot;%Y.%m.%d %H:%M&quot;)
day=$(date +&quot;%d&quot;)
hour=$(date +&quot;%H&quot;)
minute=$(date +&quot;%M&quot;)
message='---------------------------------------\n'$date' - '$num' &gt;&gt;&gt; '$ran'\n'$delim1$sourn$delim2
if ([ &quot;$day&quot; = &quot;01&quot; ] &amp;&amp; [[ &quot;$minute&quot; &lt; &quot;29&quot; ]]) || [ ! -f $dird$last ]; then
    echo &gt;$dird$last $message
else
    sed -i '1i'$message $dird$last
fi
echo $delim1$sourn$delim2
# rename the old file
cp $dest.$ext $dest'_back.'$ext
# copy the file
cat &quot;$sour&quot; &gt;$dest.$ext
echo File copied as $delim1$dest.$ext$delim2

Can you please help me to understand why this happens, and how to fix it?
Thank you very much for your attention!
Dear friends,
after a couple of hours debugging the script, finally, I have found the mistake!
The line to be used is:
list=( find &quot;$dirs&quot; -type f -name &quot;*.$ext&quot; )
it is a very subtle difference: the space after and before the parenthesis!
(even, the word busybox is useless)
Oddly in the Busybox app (I have had on my S3 Pro) the spaces are not mandatory, whilst in the Busybox Magisk module those spaces ARE mandatory!
I'm using this script for almost 8 years to have an every day different music for my wake up.
I'm using Tasker to call it just before my alarm get off, so the same file contains every day, a different song.
I have done a change also in the array index that did not began by 0
So, here it is the right script:
#!/system/bin/sh
echo
if test &quot;$1&quot; = &quot;&quot;; then
    echo &quot;Randomfile script by Uranya &lt;@uranya7x&gt; v1.5 (26.03.2021)&quot;
    echo &quot;Usage:&quot;
    echo &quot;sh randomfile.sh &lt;sourcedir&gt; &lt;extension&gt; &lt;destdir&gt;&quot;
    exit 1
fi
dirs=$1
ext=$2
dird=$3'/'
dest=$dird'random'
delim1=&quot;&quot;
delim2=&quot;&quot;
last='last.txt'
# create filename's array
IFS=$'\n'
list=( `find &quot;$dirs&quot; -type f -name &quot;*.$ext&quot;` )
# count number of files
num=${#list[@]}
# generate random number in range 1-NUM
let &quot;ran=(${RANDOM} % ${num})+ 1&quot;
echo Random from $num files is $ran
sour=${list[$ran-1]}
sourn=${sour#$dirs}
sourn=${sourn:1:${#sourn}}
date=$(date +&quot;%Y.%m.%d %H:%M&quot;)
day=$(date +&quot;%d&quot;)
hour=$(date +&quot;%H&quot;)
minute=$(date +&quot;%M&quot;)
message='---------------------------------------\n'$date' - '$num' &gt;&gt;&gt; '$ran'\n'$delim1$sourn$delim2
if ([ &quot;$day&quot; = &quot;01&quot; ] &amp;&amp; [[ &quot;$minute&quot; &lt; &quot;29&quot; ]]) || [ ! -f $dird$last ]; then
    echo &gt;$dird$last $message
else
    sed -i '1i'$message $dird$last
fi
echo $delim1$sourn$delim2
# rename the old file
cp $dest.$ext $dest'_back.'$ext
# copy the file
cat &quot;$sour&quot; &gt;$dest.$ext
echo File copied as $delim1$dest.$ext

I hope it will be useful to someone else that loves to be waked up by music.
Peace everywhere!