Shell 脚本大小写语法错误
Shell Scripting Case syntax error
当我尝试 运行 以下内容时,我在 vi 中不断收到以下语法错误“意外标记‘case’附近的语法错误”:
#!/bin/bash
if [ -z ]
then
NAME="Person"
elif [ -n ]
then
NAME=
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
for
循环条件不完整。
以this为例:
The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string.
一个例子:
for i in $( ls ); do
echo item: $i
done
您需要在脚本中迭代 NAME
。
EDIT 实际上,正如评论指出的那样,您甚至根本不需要代码中的 for
循环。你可以把它拿出来。但是如果你需要写一个专有的,请考虑到这一点。
#!/bin/bash
#Will also work with dash (/bin/sh)
#Shorter default-value assignment
#+ no need for an all-cap variable
name=""
: "${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
)。
只是语法错误,试试这个
#!/bin/bash
if [ -z ]
then
NAME="Person"
elif [ -n ]
then
NAME=
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
当我尝试 运行 以下内容时,我在 vi 中不断收到以下语法错误“意外标记‘case’附近的语法错误”:
#!/bin/bash
if [ -z ]
then
NAME="Person"
elif [ -n ]
then
NAME=
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
for
循环条件不完整。
以this为例:
The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string.
一个例子:
for i in $( ls ); do
echo item: $i
done
您需要在脚本中迭代 NAME
。
EDIT 实际上,正如评论指出的那样,您甚至根本不需要代码中的 for
循环。你可以把它拿出来。但是如果你需要写一个专有的,请考虑到这一点。
#!/bin/bash
#Will also work with dash (/bin/sh)
#Shorter default-value assignment
#+ no need for an all-cap variable
name=""
: "${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
)。
只是语法错误,试试这个
#!/bin/bash
if [ -z ]
then
NAME="Person"
elif [ -n ]
then
NAME=
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