Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux shell脚本if语句出现问题_Linux_Bash_Shell - Fatal编程技术网

Linux shell脚本if语句出现问题

Linux shell脚本if语句出现问题,linux,bash,shell,Linux,Bash,Shell,我正在尝试为运行chrunchbang linux的笔记本设置一个硬件静音按钮, 我已经让关键事件处理工作并指向如下脚本: curvol=$(amixer get Master | grep 'off') if ["$curvol" != ""] then amixer set Master unmute else amixer set Master mute fi 按下指定的按钮,如果静音,将取消静音;但是如果它还没有静音,它就不会静音 我认为问题出在if语句中,我检查命令的输出;无论if

我正在尝试为运行chrunchbang linux的笔记本设置一个硬件静音按钮, 我已经让关键事件处理工作并指向如下脚本:

curvol=$(amixer get Master | grep 'off')
if ["$curvol" != ""]
then
amixer set Master unmute 
else
amixer set Master mute
fi
按下指定的按钮,如果静音,将取消静音;但是如果它还没有静音,它就不会静音

我认为问题出在if语句中,我检查命令的输出;无论if是否返回true,它似乎总是执行unmute


任何帮助都将不胜感激!提前感谢。

您可以使用grep的返回值:

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

[
是命令的名称(有时是shell内置名)。命令后面需要一个空格才能工作:

if [ "$curvol" != "" ]

似乎只写以下内容要简单得多:

amixer set Master ${curvol:+un}mute
这相当于:

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

但是要简单得多。另外,请注意,通过使用
test
而不是
[
,语法错误变得更加困难。

可以在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)                                       
"
}

您是否检查了
$curvol
的值是否符合预期值?请在
[
命令/运算符和
]
tokenI添加了一个echo,它似乎没有使用grep,只是返回了amixer get master的全部输出。添加空格似乎起了作用,我现在觉得很傻。谢谢!别傻了-这是由
[
曾经是
test
命令的同义词。更简单的是:如果amixer获得Master | grep-q'off',则使用