Linux Bash脚本;“未找到fi行”;

Linux Bash脚本;“未找到fi行”;,linux,bash,Linux,Bash,这是我有史以来的第一个bash脚本,实际上它只是关闭了我的第二个监视器。但我一直有问题,因为当我运行它时,它总是给我错误 #!/bin/bash read -p "Do you want the 2nd monitor on or off? " ON_OFF if [$ON_OFF == on]; then xrandr --output DVI-I-3 --auto --right-of DVI-I-0 echo "done" fi if [$ON_OFF == off]; then

这是我有史以来的第一个bash脚本,实际上它只是关闭了我的第二个监视器。但我一直有问题,因为当我运行它时,它总是给我错误

#!/bin/bash


read -p "Do you want the 2nd monitor on or off? " ON_OFF

if [$ON_OFF == on]; then
xrandr --output DVI-I-3 --auto --right-of DVI-I-0
echo "done"
fi

if [$ON_OFF == off]; then
xrandr --output DVI-I-3 --off   
echo "done"
fi
当我运行它时,我得到

monitor_control.sh: 11: [[off: not found
monitor_control.sh: 16: [[off: not found
有人能解释一下为什么它不起作用吗?

这应该行得通

#!/bin/bash


echo -n "Do you want the 2nd monitor on or off? "
read ON_OFF;

if [ $ON_OFF == "on" ]; then
  xrandr --output DVI-I-3 --auto --right-of DVI-I-0
  echo "done"
fi

if [ $ON_OFF == "off" ]; then
  xrandr --output DVI-I-3 --off
  echo "done"
fi
这应该行得通

#!/bin/bash


echo -n "Do you want the 2nd monitor on or off? "
read ON_OFF;

if [ $ON_OFF == "on" ]; then
  xrandr --output DVI-I-3 --auto --right-of DVI-I-0
  echo "done"
fi

if [ $ON_OFF == "off" ]; then
  xrandr --output DVI-I-3 --off
  echo "done"
fi

您需要在
[
]
周围添加空格,因为它们在bash中是独立的命令

此外,需要在参数扩展周围使用引号,或者需要使用
[[]]
而不是
[]

也就是说,您可以使用:

if [[ $ON_OFF = on ]]
…或者您可以使用:

if [ "$ON_OFF" = on ]
否则,如果
$ON\u OFF
为空,则会出现错误

最后,如果。。。然后。。。其他的fi,例如:

if [[ $ON_OFF = on ]]; then
    xrandr --output DVI-I-3 --auto --right-of DVI-I-0
else
    xrandr --output DVI-I-3 --off   
fi
echo "done."

您需要在
[
]
周围添加空格,因为它们在bash中是独立的命令

此外,需要在参数扩展周围使用引号,或者需要使用
[[]]
而不是
[]

也就是说,您可以使用:

if [[ $ON_OFF = on ]]
…或者您可以使用:

if [ "$ON_OFF" = on ]
否则,如果
$ON\u OFF
为空,则会出现错误

最后,如果。。。然后。。。其他的fi,例如:

if [[ $ON_OFF = on ]]; then
    xrandr --output DVI-I-3 --auto --right-of DVI-I-0
else
    xrandr --output DVI-I-3 --off   
fi
echo "done."

现在我得到一个意外的操作符错误[:11:off:unexpected operator[:16:off:unexpected operator]脚本在我的slackware
root@*:~#bash--版本GNU bash,版本3.1.17(2)-发行版(i486 slackware linux GNU)版权(C)2005免费软件基金会,公司代码< /代码>你复制粘贴了吗?我在Debian上试用过,它也工作。你的分布是什么?我使用Ubuntu 10.04,GNU BASH,4.1.5版本(1)-版本(X86Y64-PC-LIUX-GNU)<代码>=< /COD>在代码> [0]中是错误的。
--这是一个bash扩展,POSIX不支持。使用一个
=
来兼容POSIX…或者,如果您想利用bash扩展,请使用
[[]]
,但使用
[]
在某种程度上不提供POSIX兼容性是愚蠢的。现在我得到了一个意外的操作符错误[:11:off:unexpected operator[:16:off:unexpected operator]脚本在我的slackware上运行良好
root@*:~#bash——版本GNU bash,版本3.1.17(2)-发布(i486 slackware linux GNU)版权(C)2005免费软件基金会,公司代码>你复制/粘贴它吗?我在Debian上试用过,它也工作。你的发行版本是什么?我使用Ubuntu 10.04,GNU BASH,4.1.5版本(1)-版本(X86Y64-PC-LIUX-GNU)<代码>=< /COD>在代码> [0]中是错误的。
--这是一个bash扩展,POSIX不支持。使用一个
=
来兼容POSIX…或者,如果您想利用bash扩展,请使用
[[]]
,但使用
[]
在某种程度上不提供POSIX兼容性是愚蠢的。从您发布的错误来看,您似乎也尝试了使用bash的
[
条件表达式语法。关于间距的相同答案也适用于该语法。从您发布的错误来看,您似乎尝试了使用bash的
[[
条件表达式语法。关于间距的相同答案也适用于该语法。实际上——POSIX
[
只允许
=
,而不允许
=
;后者是bash扩展,与最小shell不兼容……如果要使用仅限bash的扩展,只需使用
[[
,这通常会导致更少的头痛。@Charls,你是对的。但是我们已经知道他使用的是
bash
,而不是
sh
;实际上——POSIX
只允许
=
,而不是
=
[
;后者是一个bash扩展,与最小的shell不兼容……如果您打算使用仅限bash的扩展,只需使用
[[
,这通常会减少很多麻烦。@Charls,您是对的。但我们已经知道他使用的是
bash
,而不是
sh