Bash 中的表达式递归级别超出错误
Expression recursion level exceeded error in Bash
我是 bash 脚本编写的新手,正在尝试做一些练习。当我试图用 "finish" 字符串停止程序时出现这样的错误。:
line 9: ((: finish: expression recursion level exceeded (error token is "finish").
问题是什么?我也想了解我的其他缺点。
我的程序是:
#!/bin/bash
number=0
finish="finish"
temp=0
echo "Enter a number."
while true;
do
read -r number
if (( $number > $temp ))
then
temp=$number
fi
if [[ $number == $finish ]]
then
break
fi
done
echo "Largest : $temp"
引用 到相关问题,该问题与@GordonDavisson 在他的评论中提到的相同问题:
When you use a variable in an arithmetic expression, but the value is
not a number, the shell treats it as another expression to evaluate.
So if the value is a variable name, it will get the value of that
variable and use it. But in this case, you have it pointing to itself.
So to evaluate a it has to evaluate $finish
, and this keeps
repeating infinitely.
最简单的解决方案是为您的变量使用不同的名称 - 例如 finish_string="finish"
而不是 finish="finish"
。
此外,您可以使用正则表达式匹配来查看值是否为数字(注意:不需要美元符号来扩展算术表达式中的变量 ((...))
),然后进行数字比较,然后是正常字符串比较,看值是否为 'finish':
if [[ $number =~ ^[0-9]+$ ]] && ((number > temp)); then
temp=$number
elif [[ $number == $finish ]]; then
break
fi
或者,您可以在进行数字比较之前明确检查用户是否输入了数字:
if [[ $number == $finish ]]; then
break
else
[[ $number =~ ^[0-9]+$ ]] || { echo "Enter a valid number or 'finish' to stop"; continue; }
if ((number > temp)); then
temp=$number
fi
fi
运行 你的脚本 bash -x yourscript.sh
来调试它。
相关:
另请参阅:
- How to use double or single brackets, parentheses, curly braces
"finish" 不是一个数字,但您正在将它与 number.To 进行比较解决您眼前的问题,在进行比较之前检查 "finish":
while true;
do
read -r number
if [[ $number == $finish ]] ## this line moved up
then
break
fi
if (( $number > $temp ))
then
temp=$number
fi
done
当然,你还有问题,即使$number != "finish",它可能仍然不是一个数字,但这不是问题...
我是 bash 脚本编写的新手,正在尝试做一些练习。当我试图用 "finish" 字符串停止程序时出现这样的错误。:
line 9: ((: finish: expression recursion level exceeded (error token is "finish").
问题是什么?我也想了解我的其他缺点。 我的程序是:
#!/bin/bash
number=0
finish="finish"
temp=0
echo "Enter a number."
while true;
do
read -r number
if (( $number > $temp ))
then
temp=$number
fi
if [[ $number == $finish ]]
then
break
fi
done
echo "Largest : $temp"
引用
When you use a variable in an arithmetic expression, but the value is not a number, the shell treats it as another expression to evaluate. So if the value is a variable name, it will get the value of that variable and use it. But in this case, you have it pointing to itself. So to evaluate a it has to evaluate
$finish
, and this keeps repeating infinitely.
最简单的解决方案是为您的变量使用不同的名称 - 例如 finish_string="finish"
而不是 finish="finish"
。
此外,您可以使用正则表达式匹配来查看值是否为数字(注意:不需要美元符号来扩展算术表达式中的变量 ((...))
),然后进行数字比较,然后是正常字符串比较,看值是否为 'finish':
if [[ $number =~ ^[0-9]+$ ]] && ((number > temp)); then
temp=$number
elif [[ $number == $finish ]]; then
break
fi
或者,您可以在进行数字比较之前明确检查用户是否输入了数字:
if [[ $number == $finish ]]; then
break
else
[[ $number =~ ^[0-9]+$ ]] || { echo "Enter a valid number or 'finish' to stop"; continue; }
if ((number > temp)); then
temp=$number
fi
fi
运行 你的脚本 bash -x yourscript.sh
来调试它。
相关:
另请参阅:
- How to use double or single brackets, parentheses, curly braces
"finish" 不是一个数字,但您正在将它与 number.To 进行比较解决您眼前的问题,在进行比较之前检查 "finish":
while true;
do
read -r number
if [[ $number == $finish ]] ## this line moved up
then
break
fi
if (( $number > $temp ))
then
temp=$number
fi
done
当然,你还有问题,即使$number != "finish",它可能仍然不是一个数字,但这不是问题...