表达式递归级别已超出 [英] Expression recursion level exceeded

查看:59
本文介绍了表达式递归级别已超出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下示例中不知道为什么会出现错误:

Don't know why there is error in the following examples:

$ a=1; (( a > 0 )) && echo y || echo n
y
$ a=x; (( a > 0 )) && echo y || echo n
n
$ a=a; (( a > 0 )) && echo y || echo n
-bash: ((: a: expression recursion level exceeded (error token is "a")
n

推荐答案

$ a=a
( no error )
$ declare -i a
$ a=a
-bash: ((: a: expression recursion level exceeded (error token is "a")

此行为是因为 declare -i 将分配的RHS放在算术上下文中.在算术上下文中,bash递归地将变量名解引用为其值.如果名称不引用自身,则会发生无限递归.

This behavior is because declare -i puts the RHS of an assignment in arithmetic context. In arithmetic context, bash dereferences variable names to their values recursively. If the name dereferences to itself, infinite recursion ensues.

为进一步说明,只有在将有问题的变量分配给与变量相同的字符串之前,将设置为该名称的整数属性,才会得到此行为.

To clarify further, you would only get this behavior if the variable in question was assigned to a string identical to the name of the variable before setting the integer attribute on that name.

$ unset a
$ declare -i a
$ a=a
( This is fine, $a dereferences to 0. )
$ unset a
$ a=a
$ declare -i a
$ a=a
-bash: ((: a: expression recursion level exceeded (error token is "a")

这就是为什么这种情况很少发生的原因.如果您已经在算术上下文中进行赋值,则右侧无法解析为 以外的任何整数.不会发生递归.所以

That's why this is a rare occurrence. If you do the assignment when you're already in arithmetic context, then the right-hand side cannot resolve to anything other than an integer. No recursion can occur. So either

  1. (())中进行所有操作.(您也可以在那里进行作业.)
  2. 首先使用 declare -i ;不要混合类型.
  1. Do everything inside (( )). (You can do assignments in there, too.)
  2. Use declare -i first thing; don't mix types.

这篇关于表达式递归级别已超出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆