volume: swap amixer for pactl - scripts - various script and utils
HTML git clone git://z3bra.org/scripts
DIR Log
DIR Files
DIR Refs
---
DIR commit 037074bb44ff3e1e6fa0dc3ec8b69c36cb1fe0cd
DIR parent 53ce3809dc8bc525d85a7e726f4733ac19bf6ae1
HTML Author: Willy Goiffon <dev@z3bra.org>
Date: Thu, 20 Aug 2026 21:12:05 +0000
volume: swap amixer for pactl
Diffstat:
M volume | 76 +++++++++++++++++++------------
1 file changed, 47 insertions(+), 29 deletions(-)
---
DIR diff --git a/volume b/volume
@@ -1,39 +1,57 @@
#!/bin/sh
-#
-# z3bra - (c) wtfpl 2014
-
-CHANNEL=$(amixer | sed "1s/^.*'\(.*\)'.*$/\1/p;d")
-
-usage () {
- cat <<EOF
-usage: $(basename $0) [-hsla] [-+!]
- -h : print help
- -s : print on/off
- -l : print the current volume percentage (default)
- -a : print both level and state
- + : volume +5%
- - : volume -5%
- ! : toggle mute
-EOF
+
+export LC_ALL=C
+
+usage() {
+ echo "usage: $(basename $0) [-c sink/source] [volume%|toggle]" >&2
+}
+
+muted() {
+ pactl get-${control}-mute $1|cut -d' ' -f2
+}
+
+getvolume() {
+ pactl get-${control}-volume $1|grep -Eo "[0-9]+%"|uniq
}
-level() {
- amixer get $CHANNEL | sed -n 's/^.*\[\([0-9]\+\)%.*$/\1/p' | uniq
+setvolume() {
+ pactl set-${control}-volume $1 $2
}
-state() {
- amixer get $CHANNEL | sed -n 's/^.*\[\(o[nf]\+\)]$/\1/p' | uniq
+togglevolume() {
+ pactl set-${control}-mute $1 toggle
}
-# print out level and state if no argument is given
-test $# -eq 0 && echo "`level`" && exit 0
+# default to output management
+control=sink
+
+while getopts "hc:" OPT; do
+ case $OPT in
+ c) control="$OPTARG";;
+ h) usage; exit 0;;
+ *) usage; exit 1;;
+ esac
+done
+
+shift $((OPTIND - 1))
+
+case $(basename $0) in
+speakers) control=sink ;;
+microphone) control=source ;;
+esac
+
+# get default controlled device
+target=$(pactl get-default-${control})
+
+if [ "$#" -eq 0 ]; then
+ case $(muted $target) in
+ yes) echo off ;;
+ no) getvolume $target ;;
+ esac
+ exit
+fi
case $1 in
- -h) usage ;;
- -s) state ;;
- -l) level ;;
- up|+) amixer set $CHANNEL 5%+ >/dev/null;;
- down|-) amixer set $CHANNEL 5%- >/dev/null;;
- toggle|!) amixer set $CHANNEL toggle >/dev/null;;
- *) amixer set $CHANNEL $1 >/dev/null;;
+toggle|-) togglevolume $target ;;
+*) setvolume $target $1 ;;
esac