2

I'm trying to set up a hardware mute button for my notebook running chrunchbang linux, I've got the key event handling working and pointing to a script like this :

curvol=$(amixer get Master | grep 'off') if ["$curvol" != ""] then amixer set Master unmute else amixer set Master mute fi 

what happens is on pressing the button assigned, it will unmute if muted; but it won't mute if it isn't already muted.

I think the problem is in the if statement where I check for output from the command; it seems to be always doing the unmute, regardless of whether the if returns true or not.

Any help would be greatly appreciated! Thanks in advance.

4
  • Have you checked the value of $curvol is what you expect? Commented Jan 9, 2013 at 1:20
  • 1
    Include spaces around the [ command/operator and the ] token Commented Jan 9, 2013 at 1:21
  • I added an echo, it seems as if it's not using the grep, and is just returning the full output of amixer get Master Commented Jan 9, 2013 at 1:23
  • adding spaces seems to have worked, I now feel rather silly. Thank you! Commented Jan 9, 2013 at 1:24

4 Answers 4

3

[ is the name of a command (or shell builtin, sometimes). You need a space after it for it to work:

if [ "$curvol" != "" ] 
Sign up to request clarification or add additional context in comments.

1 Comment

Don't feel silly - it's a common problem brought on by the fact that [ used to be a synonym for the test command.
2

You can use the return value of grep:

amixer get Master | grep 'off' &> /dev/null if [ $? -eq 0 ] then amixer set Master unmute else amixer set Master mute fi 

1 Comment

Even simpler: use if amixer get Master | grep -q 'off'; then
2

Seems like it would be a lot simpler to just write:

amixer set Master ${curvol:+un}mute 

which is equivalent to:

if test -n "$curvol"; then amixer set Master unmute else amixer set Master mute fi 

but much less wordy. Also, note that by using test instead of [, the syntax error becomes much more difficult to make.

Comments

0

You can do it in Python. I added a BASH function to toggle the mute state. Stick it in ~/.bashrc

I'm currently using a laptop, so, I don't have multiple sound cards.
I'm not doing any error checking.

See /usr/share/doc/python-alsaaudio/examples/mixertest.py for more sample code.

# toggle Master mute function tm(){ python -c " import alsaaudio mixerObj = alsaaudio.Mixer() currentMute = mixerObj.getmute()[0] newMute = not currentMute mixerObj.setmute(newMute) " } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.