0

Could someone help me. I need a bash script to play a short sound file, wait for 30 seconds, play another sound file, wait for another 30 seconds.

(I will use it as a reminder to spend 30 seconds on each quadrant of my teeth when using a electric toothbrush.)

mpg123 /usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3
sleep 30
mpg123 /usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3
sleep 30
mpg123 /usr/share/sounds/My_Sounds/facility-alarm.mp3
sleep 30

But it only plays the first sound file. ??

Doug Smythies
  • 15,448
  • 5
  • 44
  • 61
fixit7
  • 3,127

2 Answers2

6

(I will use it as a reminder to brush each quadrant of my teeth when using a electric toothbrush.)

I have to say, this is one of the strangest reasons I have ever heard (electric toothbrushes have builtin timers these days) :D ... Probably, you'd prefer your computer to give you spoken instructions as well like so:

{
t=$(date +"%s")

while true do n=$(date +"%s") ((n<=(t+30))) && spd-say -w 'Brush Upper Left Quadrant' ((n>(t+30) && n<=(t+60))) && spd-say -w 'Brush Upper Right Quadrant' ((n>(t+60) && n<=(t+90))) && spd-say -w 'Brush Lower Left Quadrant' ((n>(t+90) && n<=(t+120))) && spd-say -w 'Brush Lower Right Quadrant' ((n>(t+120))) && break done }

Or make it less chattering like so:

{
f=(
'Brush Upper Left Quadrant'
'Brush Upper Right Quadrant'
'Brush Lower Left Quadrant'
'Brush Lower Right Quadrant'
'Congratulations, mission accomplished'
)

for i in {0..4} do spd-say -w "${f[$i]}" [ "$i" -lt 4 ] && sleep 30 done }

Or use the timeout command with your player and audio files like so:

{
timeout 30 mpg123 --loop -1 /usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3
timeout 30 mpg123 --loop -1 /usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3
timeout 30 mpg123 --loop -1 /usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3
timeout 30 mpg123 --loop -1 /usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3
}

or with sleep and the Bash builtin kill like so:

{
f=(
'/usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3'
'/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3'
'/usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3'
'/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3'
)

for i in {0..3} do mpg123 --loop -1 "${f[$i]}" &> /dev/null & p="$!" sleep 30 kill "$p" done }

Or play each file once then wait 30 seconds and move on to the next file like so:

{
f=(
'/usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3'
'/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3'
'/usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3'
'/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3'
)

for i in {0..3} do mpg123 "${f[$i]}" &> /dev/null [ "$i" -lt 3 ] && sleep 30 done }

Raffa
  • 32,237
  • I do not need it to repeatedly say "Brush Upper etc." I will edit my post to make it more clear. – fixit7 Nov 25 '23 at 14:58
  • 1
    @fixit7 I see, then probably that’s an easy job for timeout … I'll edit my answer to include that in a while when I get infront of a PC. – Raffa Nov 25 '23 at 15:10
  • 1
    @fixit7 I edited and added it from my phone anyway … I hope the formatting is OK – Raffa Nov 25 '23 at 15:31
  • @fixit7 I get it that you’re trying to best explain your requirements, but every time you edit the question, it almost technically totally changes :-) … I have tried my best to keep up with the changes and I hope that I did well. – Raffa Nov 25 '23 at 18:40
  • 1
    You did great. "The play each file once" worked perfectly. I will do some studying of some new things that I saw in your postings. – fixit7 Nov 25 '23 at 20:02
1

A few years ago I posted a bash script here in Ask Ubuntu that addresses the multiple-timer issue:

However, it allows only one custom sound file used on all the alarms.

Since then I wrote a Javascript version that runs on the Chrome or Firefox browser on any device:

The advantage of this major rewrite is you can run the timers on your smartphone and bring it to your washroom. It runs on Ubuntu of course but dragging your computer to the sink to brush your teeth is not ideal.

The other advantage of Tim-ta is you can have unlimited custom sound files uploaded and pick from them for each timer.