bash中的案例:“第4行:意外标记附近的语法错误”。

时间:2022-06-01 13:04:53

case in bash:

案例在bash中:

line 4: syntax error near unexpected token `)'

第4行:意外标记附近的语法错误。

I'm trying to use the command case in 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.

我尝试在Bash中使用命令用例(在我的树莓Pi上),但是当我运行脚本时,Bash会抛出错误。我读过很多教程,我认为我做的和他们一样,但有些事情是不正确的。

Here's my code:

这是我的代码:

#!/bin/bash
case "$1" 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 help and then make anything else output the next line.

我试着让我的脚本解释帮助,然后让其他的东西输出下一行。

(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.)

(注意,这只是一个简单的例子。这个脚本没有意义,甚至可能没有意义,它只是一个测试。

1 个解决方案

#1


3  

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

你是失踪;;在每个模式的最后:

#!/bin/bash
case "$1" 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 break statement in a programming language. They are compulsory on case.

把它看作是编程语言中的break语句。他们是强制性的。

#1


3  

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

你是失踪;;在每个模式的最后:

#!/bin/bash
case "$1" 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 break statement in a programming language. They are compulsory on case.

把它看作是编程语言中的break语句。他们是强制性的。