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

我只是一个虾纸丫 提交于 2019-12-01 08:17:27

问题


case in bash:

line 4: syntax error near unexpected token `)'

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.

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: 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:


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.



来源:https://stackoverflow.com/questions/33249368/case-in-bash-line-4-syntax-error-near-unexpected-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!