I am always asking more or less technical questions so I thought maybe I'll start my own thread where I can post about things I am trying to understand in the hope that some of you can share your knowledge, and perhaps some tutorage with me.
What am I trying to do:
I want my barscript.sh
(that I am using with sdorfehs
wm) to indicate when my speakers are muted.
What I've tried:
xev
output of my ThinkPad X230's mute button is as expected: XF86AudioMute
However, because toggling the mute botton on/off outputs the exact same information, I figure I need to write a short script?
So, I discovered the commands to mute and unmute are:
mixerctl -w outputs.master3.mute=on
mixerctl -w outputs.master3.mute=off
So with absolutely little to no scripting knowledge, I whipped up this simple script:
mute.sh:
#!/bin/sh
MUTE=$(mixerctl outputs.master3.mute | sed 's/^outputs.master3.mute=//')
if [ "${MUTE}" = "off" ]; then
RUN='mixerctl -w outputs.master3.mute=on'
else
RUN='mixerctl -w outputs.master3.mute=off'
fi
exec "$RUN"
This is clearly where I'm going wrong because the script don't work!
Running mute.sh
gives this error:
exec: mixerctl -w outputs.master3.mute=on: not found
Nevertheless, I added this keybinding to my wm config:
definekey top XF86AudioMute exec $HOME/.scripts/mute.sh
Running the command mixerctl -w outputs.master3.mute=on
in a terminal works, and after adding the relevant bits to my barscript.sh
I do get the desired output in my bar: [68%MUTE]. It's just the mute.sh
script that isn't working and my lack of basic shell scripting is clearly the reason why.
For reference, my barscript.sh:
#!/bin/sh
while true; do
# Packages
PKGS="$(pkg_info | wc -l | sed -e 's/^[ \t]*//')"
# Volume
LEV="$(mixerctl outputs.master | sed -e 's|.*,||g')"
VOL="$name$(expr \( $LEV \* 100 \) / 254)"
MUTESTATE=$(mixerctl outputs.master3.mute | sed 's/^outputs.master3.mute=//')
if [ "${MUTESTATE}" = "on" ]; then
MUTE=' MUTE'
else
MUTE=''
fi
# Battery
BAT_PERC="$(envstat -s acpibat0:charge | tail -1 | sed -e 's,.*(\([ ]*[0-9]*\)\..*,\1,g')%"
BAT_STATE="$(envstat -d acpibat0 | awk 'FNR == 10 {print $2}')"
if [ "${BAT_STATE}" = "TRUE" ]; then
STATE='+'
else
STATE='-'
fi
# Date
D="$(date '+%a %d %b %I:%M')"
# Weather
WTTR="$(cat $HOME/.scripts/weather.txt)"
# Print
echo "$D ~ $WTTR [$PKGS#][$VOL%$MUTE][$STATE$BAT_PERC]" > ~/.config/sdorfehs/bar
sleep 1
done
exit 0
The output of barscript.sh
is sent to ~/.config/sdorfehs/bar
which is then read by the wm through some sort of unix wizardry which I don't understand.
I would love to know what I'm doing wrong or how this could be done better.