简体   繁体   中英

Setting system volume in a bash script (in linux)

I need to set the system volume in a bash script, but I have no idea how to do this. Should I use alsactl ? Are there some values in /proc or /sys that I could use?

You should use the amixer tool.

Run amixer without parameters to get a list of mixer controls.
Use commands like this:

amixer set Master 50%     # set absolute
amixer set Master 2dB+    # set relative
amixer set Master unmute

You can also use pactl .

pactl set-sink-volume 0 60%  # set absolute
pactl set-sink-volume 0 +10% # set relative
pactl set-sink-mute 0 0      # unmute

In particular, the pactl set-sink-mute 0 0 unmute command works on my computer where amixer set Master unmute doesn't.

I've got a nice vol script in my ~/bin to help me do the trick ...

#!/bin/sh

export XDIALOG_NO_GMSGS=1

case ${1} in
    +|-) VOL=( $(amixer set Master 10dB${1} |tail -1|tr -d "[]") )
        Xdialog --no-buttons --title "Vol" --infobox ${VOL[3]} 50x30+32+32 ;;
    *[0-9]) VOL=( $(amixer set Master "${1}%" |tail -1|tr -d "[]") )
        Xdialog --no-buttons --title "Vol" --infobox ${VOL[3]} 50x30+32+32 ;;
    *) printf "Usage: vol [+]|[-]|[0-100]\n" >&2 ;;
esac

If you don't have Xdialog installed a simple echo ${VOL[3]} will give you some alsamixer feedback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM