尝试运行以下命令时,我在 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
最佳答案
#!/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
)。
关于linux - Shell 脚本大小写语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33313737/