为什么这个简单的 bash 代码会出现语法错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3044932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does this simple bash code give a syntax error?
提问by Tim Martin
I have the following bash code, which is copied and pasted from "bash cookbook" (1st edition):
我有以下 bash 代码,它是从“bash 食谱”(第 1 版)中复制和粘贴的:
#!/bin/bash
VERBOSE=0;
if [[ =-v ]]
then
VERBOSE=1;
shift;
fi
When I run this (bash 4.0.33), I get the following syntax error:
当我运行这个(bash 4.0.33)时,我收到以下语法错误:
./test.sh: line 4: conditional binary operator expected
./test.sh: line 4: syntax error near `=-v'
./test.sh: line 4: `if [[ =-v ]]'
Is this as simple as a misprint in the bash cookbook, or is there a version incompatibility or something else here? What would the most obvious fix be? I've tried various combinations of changing the operator, but I'm not really familiar with bash scripting.
这是否像 bash 食谱中的印刷错误一样简单,或者这里是否存在版本不兼容或其他问题?最明显的修复是什么?我尝试了各种更改运算符的组合,但我对 bash 脚本编写并不十分熟悉。
回答by
Bash uses spaces to tokenise scripts. The line:
Bash 使用空格来标记脚本。线路:
if [[ =-v ]]
should be:
应该:
if [[ = -v ]]

