Search code examples
linuxbashshellterminallinux-mint

In my shell script, how can I make the sound stop playing when my charger is plugged in?


I am new to shell scripting (and programming for that matter, please be kind). I have written a script on Linux Mint that will notify (with xcowsay) me when my battery reaches 15% and will play the sound (in the second script) "deththeme.wav" Basically I want the second part of my shell script to terminate when the charger is plugged in. I have tried using an infinite loop and the exit command, which didn't work. Everything else in my script works exactly how I want it. Here is my code:

    #!/bin/bash

    while true
    do

        PLUG=`acpi | awk {'print $3'}`

        while [ $PLUG = "Discharging," ]
        do
            BATSTAT=`acpi | cut -d , -f 2`
            if [ $BATSTAT = "98%" ]
            then
                sh sound.sh &
                xcowsay --time=10 "Plug that shit in!" | sleep 100
            else
                PLUG=`acpi | awk {'print $3'}`
            fi
        done
    done

    #!/bin/bash

    x=1
    z=1

    while [ $x = 1 ]
    do
        play -q /home/greg/Documents/deththeme.wav
        x=0
    done

    while [ $z = 1 ]
    do
        PLUG=`acpi | awk {'print $3'}`

        if [ $PLUG = "Charging," ]
        then
            z=0
        else
            :
        fi
    done

Solution

  • The problem is that play runs in the foreground, so your script waits for it to finish before proceeding to check the charging status.

    Here's a simple example that plays a sound in the background, waits five seconds, and then kills the player. You can replace the sleep with something that waits for charging instead:

    play -q somefile.wav &
    sleep 5
    kill $!