出于某种原因,此elif语句有效
elif [ $muteStat == 'no' ]; then
与此同时
elif [ $muteStat == 'no' ]; then
返回此错误
./volumeControl.sh: line 34: [ no: command not found
为什么会这样?
更新:图像中未注释的语句返回错误
代码:
#!/bin/bash
#gets all connected sinks
sinks=$(pactl list short sinks | awk '{print $1}')
#mutes sink
mute() {
pactl set-sink-mute $1 toggle
}
#adjusts volume of sink
volume() {
echo $1 $2
pactl set-sink-volume $1 $2
}
#loops through all sinks and either mutes or adjusts the volume of them
for sink in $sinks;
do
if [[ $# = 1 ]]
then
volume $sink $1
elif [[ $# = 0 ]]
then
mute $sink
fi
done
#gets current volume
currentVol=$(pactl list sinks | grep "Volume:" | awk '{print $5}' | head -n 1)
#checks if volume is muted, yes/no
muteStat=$(pactl list sinks | grep -i mute | head -n 1 | awk '{print $2}')
#sends different notification based on whether sound is muted
if [ $muteStat == 'yes' ]; then
notify-send -t 1200 "Sound is muted" "Volume is at $currentVol"
#elif [ $muteStat == 'no' ]; then
elif [ $muteStat == 'no' ]; then
notify-send -t 1200 "Volume is at $currentVol"
fi
答案 0 :(得分:1)
大概$ muteState的值为“ no”。鉴于错误消息抱怨找不到命令“ [no”(所有一个单词),因此在方括号不是简单的空格后会出现空白。
查看脚本的字符代码:od -c script.sh
并查找“有趣”字符。实际上,由于只有34行,请尝试sed -n 34p script.sh | od -c
此外,[
命令中的操作数会进行分词和文件名生成,因此变量应加引号
if [ "$muteStat" = yes ] ...
:
elif [ "$muteStat" = no ] ...
此外,==
运算符是[
的bash扩展,因此,如果您使用bash,则最好使用[[
另外,数值比较使用不同的运算符:[[ $# -eq 1 ]]