我正在尝试为运行chrunchbang linux的笔记本设置硬件静音按钮, 我有关键事件处理工作并指向这样的脚本:
curvol=$(amixer get Master | grep 'off')
if ["$curvol" != ""]
then
amixer set Master unmute
else
amixer set Master mute
fi
当按下指定的按钮时会发生什么,如果静音则会取消静音;但如果它没有静音,它就不会静音。
我认为问题出在if语句中,我检查命令的输出;无论if是否返回,它似乎总是在进行取消静音。
任何帮助将不胜感激!提前谢谢。
答案 0 :(得分:3)
[
是命令的名称(有时是内置的shell)。你需要一个空间才能工作:
if [ "$curvol" != "" ]
答案 1 :(得分:2)
您可以使用grep:
的返回值amixer get Master | grep 'off' &> /dev/null
if [ $? -eq 0 ]
then
amixer set Master unmute
else
amixer set Master mute
fi
答案 2 :(得分:2)
似乎只是写起来会简单得多:
amixer set Master ${curvol:+un}mute
相当于:
if test -n "$curvol"; then
amixer set Master unmute
else
amixer set Master mute
fi
但不那么罗嗦。另请注意,使用test
代替[
,语法错误变得更加困难。
答案 3 :(得分:0)
你可以在Python中完成。我添加了一个BASH函数来切换静音状态。 把它粘在〜/ .bashrc
中我目前正在使用笔记本电脑,因此,我没有多张声卡 我没有做任何错误检查。
请参阅/usr/share/doc/python-alsaaudio/examples/mixertest.py 了解更多示例代码。
# toggle Master mute
function tm(){
python -c "
import alsaaudio
mixerObj = alsaaudio.Mixer()
currentMute = mixerObj.getmute()[0]
newMute = not currentMute
mixerObj.setmute(newMute)
"
}