1

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 "$dirs" -type f -name *.$ext`)

returns an empty array.

This is the complete script:

#!/system/bin/sh
echo
if test "$1" = ""; then
     echo "Randomfile script by Uranya <@uranya7x> v1.4 01.01.2021"
     echo "Usage:"
     echo "sh randomfile.sh <sourcedir> <extension> <destdir>"
     exit 1
fi
dirs=$1
ext=$2
dird=$3'/'
dest=$dird'random'
delim1=""
delim2=""
last='last.txt'
# create filename's array
IFS=$'\n'

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

# count number of files
num=${#list[@]}
# generate random number in range 1-NUM
let "ran=(${RANDOM} % ${num})+ 1"
echo Random from $num files is $ran
sour=${list[ran]}
sourn=${sour#$dirs}
sourn=${sourn:1:${#sourn}}
date=$(date +"%Y.%m.%d %H:%M")
day=$(date +"%d")
hour=$(date +"%H")
minute=$(date +"%M")
message='---------------------------------------\n'$date' - '$num' >>> '$ran'\n'$delim1$sourn$delim2
if ([ "$day" = "01" ] && [[ "$minute" < "29" ]]) || [ ! -f $dird$last ]; then
    echo >$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 "$sour" >$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!

3
  • Having done some tests I have found this: opening a root privileged terminal and executing --- echo find /storage/7BC3-1805/Music/MP3/Abba -type f -name *.mp3 --- it returns two strings containing the names of files inside that folder, but putting it in my script continues to return an empty array, so the issue is not in the access to the folder, but in the syntax, I guess (the code find is inside backticks) so it is as: echo <BT>find.... *.mp3<BT> Commented Mar 25, 2021 at 19:46
  • what shell you are running the script? android default is mksh (MirBSD korn shell). only bash support arrays, set shebang to something like #!/data/data/com.termux/files/usr/bin/bash
    – alecxs
    Commented Mar 26, 2021 at 11:06
  • @alecxs Thank you for pointing it! Commented Mar 26, 2021 at 11:39

1 Answer 1

1

Dear friends,

after a couple of hours debugging the script, finally, I have found the mistake! The line to be used is: list=( find "$dirs" -type f -name "*.$ext" )

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 "$1" = ""; then
    echo "Randomfile script by Uranya <@uranya7x> v1.5 (26.03.2021)"
    echo "Usage:"
    echo "sh randomfile.sh <sourcedir> <extension> <destdir>"
    exit 1
fi
dirs=$1
ext=$2
dird=$3'/'
dest=$dird'random'
delim1=""
delim2=""
last='last.txt'
# create filename's array
IFS=$'\n'
list=( `find "$dirs" -type f -name "*.$ext"` )
# count number of files
num=${#list[@]}
# generate random number in range 1-NUM
let "ran=(${RANDOM} % ${num})+ 1"
echo Random from $num files is $ran
sour=${list[$ran-1]}
sourn=${sour#$dirs}
sourn=${sourn:1:${#sourn}}
date=$(date +"%Y.%m.%d %H:%M")
day=$(date +"%d")
hour=$(date +"%H")
minute=$(date +"%M")
message='---------------------------------------\n'$date' - '$num' >>> '$ran'\n'$delim1$sourn$delim2
if ([ "$day" = "01" ] && [[ "$minute" < "29" ]]) || [ ! -f $dird$last ]; then
    echo >$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 "$sour" >$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!

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .