不可能的 bash:语法错误:无效的算术运算符(错误标记为“”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25348918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Impossible bash: syntax error: invalid arithmetic operator (error token is "")
提问by Afr
I know there have been a lot of similar questions here but this is pretty special, please read on.
我知道这里有很多类似的问题,但这很特别,请继续阅读。
I have a bash script that does nothing but comparing two numbers, basically like that:
我有一个 bash 脚本,它只比较两个数字,基本上是这样的:
[[ 1408039118 -lt 1401215749 ]]
Now, running that script throws the following error:
现在,运行该脚本会引发以下错误:
/usr/bin/pacaur: line 179: 1408039118: syntax error: invalid arithmetic operator (error token is "")
So I understand something must be wrong, this is my line 179:
所以我明白一定有问题,这是我的第 179 行:
[[ "${depsAlastmodified[$i]}" -lt 1401215749 ]] && note "f" $"no AUR metadata for ${colorR}${depsAname[$i]}${reset} package"
Running this through bash -x
shows:
通过bash -x
节目运行这个:
+ [[ 1408039118 -lt 1401215749 ]]
/usr/bin/pacaur: line 179: 1406628774: syntax error: invalid arithmetic operator (error token is "")
There is really nothing visible wrong with that. I tried some further debugging using od -c
on that variable:
这真的没有什么明显的错误。我尝试使用od -c
该变量进行一些进一步的调试:
echo ${depsAlastmodified[$i]} | od -c
The output is:
输出是:
+ echo '1408039118'
+ od -c
0000000 1 4 0 8 0 3 9 1 1 8 033 [ m 033 [ K
0000020 \n
0000021
But now I'm not sure how to read this. I understand the newline character is belonging to the echo command. But what is 033 [ m 033 [ K
exactly? And does this belong to my issue?
但现在我不知道如何阅读这个。我知道换行符属于 echo 命令。但究竟是033 [ m 033 [ K
什么?这属于我的问题吗?
I also tried running that number through bc
:
我还尝试通过bc
以下方式运行该数字:
echo ${depsAlastmodified[$i]} | bc | od -c
This is the output:
这是输出:
+ echo '1408039118'
+ bc
+ od -c
(standard_in) 1: illegal character: ^[
(standard_in) 1: syntax error
(standard_in) 1: illegal character: ^[
(standard_in) 1: illegal character: K
0000000
Something is wrong with that variable. What else could I try? How to fix this?
该变量有问题。我还能尝试什么?如何解决这个问题?
Just for reference, this is the full issue history.
仅供参考,这是完整的问题历史。
采纳答案by Afr
It has something to do with grep adding colorescape stuff at the end of line,
It's really no easy task to track this down and fix this.
For reference, if anyone else comes across this issue:
I had GREP_OPTIONS="--color=always"
in my ~\.bashrc
file, to fix this, you need to do the following:
追踪并解决这个问题真的不是一件容易的事。作为参考,如果其他人遇到此问题:GREP_OPTIONS="--color=always"
我的~\.bashrc
文件中有,要解决此问题,您需要执行以下操作:
- Uninstall pacaur, remove the complete /tmp/pacaurtmp-*/ directory.
- Put
GREP_OPTIONS="--color=never"
(orauto
) into your~/.bashrc
andsource ~/.bashrc
it. - Reinstall pacaur and do a whole system upgrade
pacaur -Syu
- 卸载pacaur,删除完整的/tmp/pacaurtmp-*/ 目录。
- 将
GREP_OPTIONS="--color=never"
(或auto
)放入您的~/.bashrc
和source ~/.bashrc
它。 - 重新安装pacaur并进行整个系统升级
pacaur -Syu
回答by anubhava
Looks like you have trailing characters in your array.
看起来您的数组中有尾随字符。
Try this with tr -cd '[[:digit:]]'
which will delete all non digits from input:
试试这个,tr -cd '[[:digit:]]'
这将从输入中删除所有非数字:
echo "${depsAlastmodified[$i]}" | tr -cd '[[:digit:]]' | od -c
It should give:
它应该给出:
0000000 1 4 0 8 0 3 9 1 1 8
0000012