这是我尝试运行脚本时遇到的错误
~bin/killp: line 7: [[ menubar: command not found
~bin/killp: line 11: [[ menubar: command not found
~bin/killp: line 11: [[: command not found
~bin/killp: line 15: [[ menubar: command not found
~bin/killp: line 15: [[: command not found
~bin/killp: line 15: [[ menubar: command not found
~bin/killp: line 19: conditional binary operator expected
~bin/killp: line 19: syntax error near `Dock'
~bin/killp: line 19: `if [[ $1 == Dock ]]; then'
这就是这些线条的样子
if [[ $1 == Desktop ]] || [[ $1 == Finder ]]; then
killall Finder
fi
唯一的区别是Desktop
和Finder
。menubar
是我传递给脚本的参数,又名$1
.
答案1
根据错误消息,您的脚本可能有一些隐藏的不间断空格字符(例如您输入了⌥- space)
例如,错误实际上是这样的:
~bin/killp: line 7: [[@menubar: command not found
~bin/killp: line 11: [[@menubar: command not found
~bin/killp: line 11: @[[: command not found
~bin/killp: line 15: [[@menubar: command not found
~bin/killp: line 15: @[[: command not found
~bin/killp: line 15: @[[@menubar: command not found
~bin/killp: line 19: conditional binary operator expected
~bin/killp: line 19: syntax error near `Dock'
~bin/killp: line 19: `if [[ $1 == Dock ]]; then' <-- Somewhere, not sure where.
我用@替换了不可见的字符。
答案2
根据我的经验,类似这样的事情可以通过在代码中查找一两行并找到我搞砸的东西来解决。
我刚刚使用 vim 在 Mac 上输入了您提供的内容(使用 echo 而不是 Killall),它运行得很好,如下:
$ more test.sh
#!/bin/sh
if [[ $1 == Desktop ]] || [[ $1 == Finder ]]; then
echo "Finder"
fi
在运行 cygwin 的 Windows 机器上:
petro@<blur> ~
$ ./test.sh
petro@<blur> ~
$ ./test.sh stuffing
petro@<blur> ~
$ ./test.sh Finder
Finder
因此,编写的代码看起来不错,您的问题可能就在上面。