bash 中的情况:“第 4 行:意外标记附近的语法错误‘)’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33249368/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:46:48  来源:igfitidea点击:

case in bash: "line 4: syntax error near unexpected token `)'"

linuxbashsyntax-errorcase

提问by Eric Reed

case in bash:

bash中的情况:

line 4: syntax error near unexpected token `)'

第 4 行:意外标记“)”附近的语法错误

I'm trying to use the command casein Bash (on my Raspberry Pi again), but when I run my script, Bash spits out errors. I've read over many tutorials and I think I'm doing the same thing as them, but something's just not right.

我正在尝试case在 Bash 中使用该命令(再次在我的 Raspberry Pi 上),但是当我运行我的脚本时,Bash 吐出错误。我已经阅读了很多教程,我认为我正在做和他们一样的事情,但有些地方不对。

Here's my code:

这是我的代码:

#!/bin/bash
case "" in
        help) echo "You asked for help. Sorry, I'm busy."
        *) echo "You didn't say anything. Try 'help' as the first argument."
esac

Here's the output (the filename is newmkdir and I ran it with no arguments):

这是输出(文件名是 newmkdir,我不带参数运行它):

./newmkdir: line 4: syntax error near unexpected token `)'
./newmkdir: line 4: `   *) echo "You didn't say anything. Try 'help' as the first argument."'

I'm trying to have my script interpret helpand then make anything else output the next line.

我正在尝试解释我的脚本help,然后在下一行输出任何其他内容。

(Note this is just an example of a glitched script. This script has no meaning and might not even make sense, it's just a test.)

(请注意,这只是故障脚本的一个示例。这个脚本没有意义,甚至可能没有意义,它只是一个测试。)

回答by Alvaro Gutierrez Perez

You are missing ;;at the end of each pattern:

;;在每个模式的末尾都缺少:

#!/bin/bash
case "" in
        help)
            echo "You asked for help. Sorry, I'm busy."
            ;;
        *)
            echo "You didn't say anything. Try 'help' as the first argument."
            ;;
esac

Think of it as a breakstatement in a programming language. They are compulsory on case.

将其视为break编程语言中的语句。他们是强制性的case