Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # Movesoundoutput moves all sound outputs to a device, according to the given parameters.
- #
- # If the first parameter is '--list' , it only lists all known or filtered sound outputs.
- # Otherwise, the first parameter tells which sound output to switch to.
- # Only a partial name of the device or it's description is needed to select it.
- #
- function RequiredPrograms () {
- # Each program listed must exist, otherwise throw an error and exit.
- for List in "$@"
- do
- if ! command -v "$List" > /dev/null ;then
- echo "Error: Required command $List not found" >&2
- exit 1
- fi
- done
- }
- function ParsePacmd() {
- # $1 = search term
- list=$(pacmd list-sinks)
- while true; do
- nr=$(echo "$list" | grep -ni -m1 'index:' | cut -f1 -d':')
- [ "$nr" == "" ] && break
- PrintValidSink "$(head -n$nr <<< $list)" "$1"
- nrinc=$((nr+1))
- list=$(echo "$list" | tail -n+$nrinc)
- done
- PrintValidSink "$list" "$1"
- }
- function PrintValidSink() {
- # $1 = Sink data
- # $2 = Search item
- if grep 'device.master_device' <<< "$1" >/dev/null; then
- return 0
- fi
- if [ -n "$2" ] && ! grep -i "$2" <<< "$1" >/dev/null; then
- return 0
- fi
- GetSinkName "$1"
- }
- function GetSinkName() {
- # $1 = Sink Data
- echo "$1" | grep '^\s*name:' | cut -f2 -d'<' | cut -f1 -d'>'
- }
- function ErrorExit() {
- echo Error: $* >&2
- exit 1
- }
- # These programs must exist.
- RequiredPrograms pacmd
- # When --list parameter is applied, only list current sinks or filtered sinks.
- if [ "$1" == "--list" ]; then
- shift
- ParsePacmd "$1"
- exit 0
- fi
- if [ "$1" == "" ]; then
- ErrorExit "No device string given"
- fi
- # Find the correct sink.
- sink=$(ParsePacmd "$1")
- if [ $(wc -w <<< "$sink") -ne 1 ]; then
- ErrorExit "Not exactly one sink filtered"
- fi
- # Change the default audio sink.
- pacmd set-default-sink "$sink"
- # Move all inputs to the new sink
- for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
- do
- pacmd move-sink-input $app "$sink"
- done
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment