random directory runner

torquebar

New Member
Joined
Jun 18, 2025
Messages
4
Reaction score
0
Credits
36
Greetings, I just came over from LQO where I can't even log-in anymore

In an effort to build a guitar chords quiz I'm trying to code a bash script that

1- builds an array of one directory's contents
(about 300 guitar chord notation images images [prompts])
2- selects a random image from the array
3- screens it
4- sleeps X seconds
5- replaces image by one of same name from another directory
(the same about 300 guitar chord but this time notation + chordbox images)
6- loops Y times a cue/validation .mp3 sharing the name root from yet another directory
7- removes element from array
8- returns to 2 to select another random file from remainder of array

Latest edit of the rudimentary beginnings:


Bash:
#!/bin/bash
# repopulate promt directory from standard
cp /0/data/music/chordquiz/promptsrc/* /0/data/music/chordquiz/prompt
directory_path="/0/data/music/chordquiz/prompt"
while [ "$(ls -A "$directory_path")" ]

do
cd /0/data/music/chordquiz/prompt
array=( * )     #make new array from directory contents
#
#echo "${array[@]}" # will dump all elements of the array
size=${#array[@]}   # assign size of array to variable 'size'
rfromsize=$(($RANDOM % size))  # # Pick an array element, rfromsize WAS 'index'before
echo $rfromsize $size > ~/r1-size1.txt
longelement=${array[$rfromsize]}
element=${longelement::-4}   # remove (.png) extension extension from longelement to make element
#
# SHOW the PROMT image for element:
###################################
feh -x -g 540x924+1500+500 --zoom 300  /0/data/music/chordquiz/prompt/$element.png &
pid=$!      # get pid of THIS command now backgrnd process
sleep 1s    # wait X seconds
kill $pid   # close this pid
#
# show image 2 of element:
feh -x -g 540x1596+1500+300 --zoom 300 /0/data/music/chordquiz/diags/$element.png &
pid2=$!      # get pid of THIS command now backgrnd process
sleep 10s    # wait X seconds
#
# LOOP the mp3 XY times while still showing image 2 of element
#mpg123 --loop 3 /0/data/music/chordquiz/mp3/$element.mp3
# only have the 1 mp3 so far
#mpg123 --loop 3 /0/data/music/chordquiz/mp3/B_3.mp3
pid3=$!      # get pid of THIS command now backgrnd process
kill $pid    # close this pid
kill $pid2   # close this pid
kill $pid3   # close this pid
#
# remove file from prompt directory
rm /0/data/music/chordquiz/prompt/"$longelement"
done
exit

Because my bash in no flash in the pan I need to keep it extremely formatted and tutorial/verbose

All help appreciated, the above WORKS but is embare-assingly rudimentary
 
Last edited:


Greetings, I just came over from LQO where I can't even log-in anymore

In an effort to build a guitar chords quiz I'm trying to code a bash script that

1- builds an array of one directory's contents
(about 300 guitar chord notation images images [prompts])
2- selects a random image from the array
3- screens it
4- sleeps X seconds
5- replaces image by one of same name from another directory
(the same about 300 guitar chord but this time notation + chordbox images)
6- loops Y times a cue/validation .mp3 sharing the name root from yet another directory
7- removes element from array
8- returns to 2 to select another random file from remainder of array

Latest edit of the rudimentary beginnings:


Bash:
#!/bin/bash
# repopulate promt directory from standard
cp /0/data/music/chordquiz/promptsrc/* /0/data/music/chordquiz/prompt
directory_path="/0/data/music/chordquiz/prompt"
while [ "$(ls -A "$directory_path")" ]

do
cd /0/data/music/chordquiz/prompt
array=( * )     #make new array from directory contents
#
#echo "${array[@]}" # will dump all elements of the array
size=${#array[@]}   # assign size of array to variable 'size'
rfromsize=$(($RANDOM % size))  # # Pick an array element, rfromsize WAS 'index'before
echo $rfromsize $size > ~/r1-size1.txt
longelement=${array[$rfromsize]}
element=${longelement::-4}   # remove (.png) extension extension from longelement to make element
#
# SHOW the PROMT image for element:
###################################
feh -x -g 540x924+1500+500 --zoom 300  /0/data/music/chordquiz/prompt/$element.png &
pid=$!      # get pid of THIS command now backgrnd process
sleep 1s    # wait X seconds
kill $pid   # close this pid
#
# show image 2 of element:
feh -x -g 540x1596+1500+300 --zoom 300 /0/data/music/chordquiz/diags/$element.png &
pid2=$!      # get pid of THIS command now backgrnd process
sleep 10s    # wait X seconds
#
# LOOP the mp3 XY times while still showing image 2 of element
#mpg123 --loop 3 /0/data/music/chordquiz/mp3/$element.mp3
# only have the 1 mp3 so far
#mpg123 --loop 3 /0/data/music/chordquiz/mp3/B_3.mp3
pid3=$!      # get pid of THIS command now backgrnd process
kill $pid    # close this pid
kill $pid2   # close this pid
kill $pid3   # close this pid
#
# remove file from prompt directory
rm /0/data/music/chordquiz/prompt/"$longelement"
done
exit

Because my bash in no flash in the pan I need to keep it extremely formatted and tutorial/verbose

All help appreciated, the above WORKS but is embare-assingly rudimentary
I'm thinking our moderator @JasKinasis may be able to help with your script.
 
my desktop is waiting for a replacement motherboard; everything's on hold UFN
Understood.
Let us know when your pc is up and running again.
 
New motherboard is running (hope it will continue doing so).
 
This isn't something I'm versed in. My programming abilities are poor and outdated.

That said, I'm not sure that Bash is the appropriate language.

We do have a musician mod who is also a programmer. (They play percussion instruments, AFAIK.) I'll ping them.

@JasKinasis
 
No, no, no, I am NOT going to learn yet another frickin' language; having been otherwise quadrilingual and then wasting time in Amiga-Basic, PC-basic, Perl and lately bash it's time to put end end to all this nonsense. I'll do what I can in bash .......or do nothing :-)
 
This isn't something I'm versed in. My programming abilities are poor and outdated.

That said, I'm not sure that Bash is the appropriate language.

We do have a musician mod who is also a programmer. (They play percussion instruments, AFAIK.) I'll ping them.

@JasKinasis
Sorry I'm a little late to the party. I've been on holiday for the last 3 weeks. Finally back to work now.

I'll take a look at the OP's bash script later and will see if I can suggest any improvements to it.

And yes, I'm primarily a drummer and percussionist. But FYI, I can also play guitar, bass, ukulele and a bit of keyboard. I don't have much knowledge of music theory, but I know enough to be dangerous! Ha ha!
 
Last edited:


Follow Linux.org

Members online


Top