Shell Scripting Case语法错误

时间:2015-10-24 01:57:26

标签: linux bash vi

在尝试运行以下内容时,我在vi中不断收到以下语法错误“语法错误接近意外令牌`case'”:

 #!/bin/bash
if [ -z $1 ]
then
        NAME="Person"
elif [ -n $1 ]
then
        NAME=$1
fi

for NAME
case $NAME in
        "Alice") echo "$NAME is a member of the name group.";;
        "Bob") echo "$NAME is a member of the name group.";;
        "Charlie") echo "$NAME is a member of the name group.";;
        "Quan") echo "$NAME is a member of the name group.";;
        "Brandon") echo "$NAME is a member of the name group.";;
        *) echo "Sorry, That $NAME is not a member of the name group.";;
esac

3 个答案:

答案 0 :(得分:1)

#!/bin/bash
#Will also work with dash (/bin/sh)

#Shorter default-value assignment
#+ no need for an all-cap variable
name="$1" 
: "${name:=Person}"

#`for name` doesn't belong here
case "$name" in
        "Alice") echo "$name is a member of the name group.";;
        "Bob") echo "$name is a member of the name group.";;
        "Charlie") echo "$name is a member of the name group.";;
        "Quan") echo "$name is a member of the name group.";;
        "Brandon") echo "$name is a member of the name group.";;
        *) echo "Sorry, That $name is not a member of the name group.";;
esac

全方位变量通常用于:

  • 导出或继承自环境的变量
  • 配置shell的变量

如果两者都不适用,则无需全部上限。

默认情况下引用"$variables"是一种很好的做法,除非您特别想要在空格上进行拆分(或更准确地$IFS)。

答案 1 :(得分:1)

只是语法错误,试试这个

#!/bin/bash
if [ -z $1 ]
then
        NAME="Person"
elif [ -n $1 ]
then
        NAME=$1
fi

case $NAME in
        "Alice") echo "$NAME is a member of the name group.";;
        "Bob") echo "$NAME is a member of the name group.";;
        "Charlie") echo "$NAME is a member of the name group.";;
        "Quan") echo "$NAME is a member of the name group.";;
        "Brandon") echo "$NAME is a member of the name group.";;
        *) echo "Sorry, That $NAME is not a member of the name group.";;
esac

答案 2 :(得分:0)

<script> $("table tr").each(function () { var cells = $(this).find('td:empty'); if (cells.length > 0) { console.log('empty'); $(this).addClass('nodisplay'); } }); </script> 循环条件不完整。

请参阅this作为示例:

  

for循环与其他编程语言略有不同。基本上,它让你在字符串中迭代一系列“单词”。

一个例子:

for

您需要在脚本中进行for i in $( ls ); do echo item: $i done 的迭代。

编辑实际上,正如评论指出的那样,您甚至根本不需要代码中的NAME循环。你可以把它拿出来。但如果你需要写一个适当的,请考虑到这一点。