0

I'm working on a bash script that runs in an infinite 1 second loop, where if it detects that the computer is plugged into AC power, it plays the file plugin.ogg with ogg123, and if it is not plugged in, it plays a silent file Silence.ogg. However, I have a few issues. First, it seems to no longer loop (probably a side effect of the wait I had to throw in in order to avoid a looping charger input sound after the first detection. Secondly, it doesn't seem to want to change to the / directory and it therefore (I'm not sure if due to the / error or something else) can't find the files to play. Please help fix this, thanks!

#!/bin/bash
"cd /"

while true
do
    if on_ac_power; then 
        "ogg123 plugin.ogg"               ## Laptop on power
    else
        "ogg123 Silence.ogg"          ## Laptop on battery
    fi
    sleep 10                       ## wait 10 sec before repeating
done

Terminal output if run while computer is unplugged: /home/brenden_mcfarling_live/Desktop/ac0.sh: line 3: cd /: No such file or directory

/home/brenden_mcfarling_live/Desktop/ac0.sh: line 10: ogg123 Silence.ogg: command not found

Terminal output when run while plugged in: /home/brenden_mcfarling_live/Desktop/ac0.sh: line 3: cd /: No such file or directory

/home/brenden_mcfarling_live/Desktop/ac0.sh: line 8: ogg123 plugin.ogg: command not found

Any help would be appreciated!

2 Answers 2

0

At first: Why are you changing directory to root / ? Are your files located there ?

Remove the qoutes ".." from your command. So that it looks like

ogg123 plugin.ogg

Make sure that you have installed the package vorbis-tools

# sudo apt-get install vorbis-tools

2
  • My files are there. However, I'm still getting issues with them, so I'm going to place a local copy in the home directory, then into etc/skel (for each new user)
    – SYGMAH
    Jan 10, 2018 at 18:08
  • Now, I get the output of "Cannot open (name).ogg"
    – SYGMAH
    Jan 10, 2018 at 18:14
0

As @JonnyTischbein said: remove the quotes from your commands because else the shell will try to execute the literal command "cd /", i.e. it will search for an executable file called "cd /" and execute it. Of course, it does not exist and you get bash: cd /: No such file or directory.

The same goes for "ogg123 plugin.ogg". It will try to execute the file "ogg123 plugin.ogg" which also doesn't exist. Given your other error messages I assume that the *.ogg files reside in /home/brenden_mcfarling_live/Desktop (and not in /). Thus, after changing to / the files cannot be found any more and you need to supply the full path.

Put together:

#!/bin/bash
cd /

while true
do
    if on_ac_power; then 
        ## Laptop on power
        ogg123 /home/brenden_mcfarling_live/Desktop/plugin.ogg
    else
        ## Laptop on battery
        ogg123 /home/brenden_mcfarling_live/Desktop/Silence.ogg
    fi
    ## wait 10 sec before repeating
    sleep 10
done

If the path-to or filename-of the sound files contained spaces, then you can use quotes but only around the filename:

ogg123 "/home/brenden_mcfarling_live/Desk Top/Very silent sound.ogg"

You may also want to have a look at this question (and its answer) at Unix & Linux. There they say how to immediately catch the event of plugging/unplugging the AC adapter so you don't need to poll every 10 seconds but get triggered instead.

I admit, it's a bit complicated at first sight but the idea is:

  • Do an educated guess what events happen when the cord is plugged in and out
  • Write a script that gets called for "plugged in" (that plays your first sound file)
  • Write a script that gets called for "plugged out" (that plays your second sound file)
  • Create two rules in a new file below /etc/udev/rules.d that say "when plugged in, then run script1.sh" and "when unplugged, then run script2.sh"

You must log in to answer this question.

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