意外标记“{”附近存在语法错误

意外标记“{”附近存在语法错误

每次我运行新的虚拟终端时,它都会以

bash: /usr/share/bash-completion/bash_completion: line 226: syntax error near unexpected token `}'
bash: /usr/share/bash-completion/bash_completion: line 226: `}'
kimifish@kimipc:~$ 

但该文件bash_completion似乎没问题(当然不是)。抱歉那些垂直条,这只是 vim 选项卡突出显示

     ...
     186 # Assign variables one scope above the caller
     187 # Usage: local varname [varname ...] &&
     188 #        _upvars [-v varname value] | [-aN varname [value ...]] ...
     189 # Available OPTIONS:
     190 #     -aN  Assign next N values to varname as array
     191 #     -v   Assign single value to varname
     192 # Return: 1 if error occurs
     193 # See: http://fvue.nl/wiki/Bash:_Passing_variables_by_reference
     194 _upvars()
     195 {
     196     if ! (( $# )); then
     197     |   echo "${FUNCNAME[0]}: usage: ${FUNCNAME[0]} [-v varname"\
     198     |   |   "value] | [-aN varname [value ...]] ..." 1>&2
     199     |   return 2
     200     fi
     201     while (( $# )); do
     202     |   case $1 in
     203             -a*)
     204     |   |   |   # Error checking
     205     |   |   |   [[ ${1#-a} ]] || { echo "bash: ${FUNCNAME[0]}: \`$1': missing"\
     206     |   |   |   |   "number specifier" 1>&2; return 1; }
     207     |   |   |   printf %d "${1#-a}" &> /dev/null || { echo "bash:"\
     208     |   |   |   |   "${FUNCNAME[0]}: \`$1': invalid number specifier" 1>&2
     209     |   |   |   |   return 1; }
     210     |   |   |   # Assign array of -aN elements 
     211     |   |   |   [[ "$2" ]] && unset -v "$2" && eval $2=\(\"\${@:3:${1#-a}}\"\) &&
     212     |   |   |   shift $((${1#-a} + 2)) || { echo "bash: ${FUNCNAME[0]}:"\
     213     |   |   |   |   "\`$1${2+ }$2': missing argument(s)" 1>&2; return 1; }
     214     |   |   |   ;;
     215             -v) 
     216     |   |   |   # Assign single value
     217     |   |   |   [[ "$2" ]] && unset -v "$2" && eval $2=\"\$3\" &&
     218     |   |   |   shift 3 || { echo "bash: ${FUNCNAME[0]}: $1: missing"\
     219     |   |   |   "argument(s)" 1>&2; return 1; }
     220     |   |   |   ;;
     221             *)  
     222     |   |   |   echo "bash: ${FUNCNAME[0]}: $1: invalid option" 1>&2
     223     |   |   |   return 1 ;;
     224         esac
     225     done
     226 }
     ...

我完全不知道在哪里寻找错误......

~/.bashrc我关于 bash-completion 的部分内容:

  91 if ! shopt -oq posix ; then
  92   if [ -f /usr/share/bash-completion/bash_completion ]; then
  93     . /usr/share/bash-completion/bash_completion
  94   elif [ -f /etc/bash_completion ]; then
  95     . /etc/bash_completion
  96   fi
  97 fi

按部分删除代码显示了一些信息:切割“ while”循环可以消除错误,但仅切割“ case”结构则不能。 =/

答案1

一种可能的解释是你这样做

alias done='something'

在采购之前/usr/share/bash-completion/bash_completion

这可以解释该错误消息,因为它bash会抱怨函数已关闭,而while循环尚未关闭(因为缺少done被替换something)。

不要使用done或 任何 shell 关键字作为别名(尽管您可以定义一些仪器别名,只要alias do='do ((loop_level++));'; alias done='((loop_level--)); done'它们保留 shell 结构即可)。

相关内容