Bash: Volume notification script

I decided it might be smart of me to post this script on my blog so i knew i had it in the future, as well as others might find it useful. The script was originally made by the user abarilla from archlinux forums which I have later modified slightly.

Abarilla said:

I created a script for handling my volume control which uses the notify-osd package from AUR.  This is the newfangled notifications from Ubuntu.  It’s nothing fancy so the notify-osd-bzr package will probably work fine as well.

This is for alsa but with some minor mods it should work with oss.

The format for the command is: vol.sh {up|down|mute} [-i increment] [-m mixer]

Increment is either a percent or number just like you would pass to amixer.  Defaults to 5%
Mixer is the name of the control to change.  Defaults to Master

So to sum it up, requirements are ALSA and notify-osd. Anyway here’s the almost original script:

 
#!/bin/sh
 
usage="usage: $0 -c {up|down|mute} [-i increment] [-m mixer]"
command=
increment=5%
mixer=Master
 
while getopts i:m:h o
do case "$o" in
    i) increment=$OPTARG;;
    m) mixer=$OPTARG;;
    h) echo "$usage"; exit 0;;
    ?) echo "$usage"; exit 0;;
esac
done
 
shift $(($OPTIND - 1))
command=$1
 
if [ "$command" = "" ]; then
    echo "usage: $0 {up|down|mute} [increment]"
    exit 0;
fi
 
display_volume=0
 
if [ "$command" = "up" ]; then
    display_volume=$(amixer set $mixer $increment+ unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
fi
 
if [ "$command" = "down" ]; then
    display_volume=$(amixer set $mixer $increment- unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
fi
 
icon_name=""
 
if [ "$command" = "mute" ]; then
    amixer get $mixer | grep "\[on\]" > /dev/null
    if [ $? -eq 0 ]; then
        display_volume=0
        icon_name="notification-audio-volume-muted"
        amixer set $mixer mute
    else
        display_volume=$(amixer set $mixer unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
    fi
fi
 
if [ "$icon_name" = "" ]; then
    if [ "$display_volume" = "0" ]; then
        icon_name="notification-audio-volume-off"
    else
        if [ "$display_volume" -lt "33" ]; then
            icon_name="notification-audio-volume-low"
        else
            if [ "$display_volume" -lt "67" ]; then
                icon_name="notification-audio-volume-medium"
            else
                icon_name="notification-audio-volume-high"
            fi
        fi
    fi
fi
notify-send " " -i $icon_name -h int:value:$display_volume -h string:synchronous:volume

About Torstein Skulbru

23 year old, Bachelor ICT(Information and Communication Technology) student at University of Bergen (NO)