bash中的情况:“第4行:意外令牌')'附近的语法错误" [英] case in bash: "line 4: syntax error near unexpected token `)'"
问题描述
bash中的情况:
第4行:意外令牌')'附近的语法错误
line 4: syntax error near unexpected token `)'
我试图在Bash中使用命令case
(再次在Raspberry Pi上),但是当我运行脚本时,Bash吐出错误.我已经阅读了许多教程,我认为我正在做与他们相同的事情,但是有些事情是不正确的.
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.
这是我的代码:
#!/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
这是输出(文件名是newmkdir,我没有参数就运行了它):
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."'
我试图让我的脚本解释help
,然后使其他任何内容输出到下一行.
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.)
推荐答案
在每个模式的结尾您都缺少;;
:
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
将其视为编程语言中的break
语句.他们是case
上的必修课.
Think of it as a break
statement in a programming language. They are compulsory on case
.
这篇关于bash中的情况:“第4行:意外令牌')'附近的语法错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!