Bash:值对于基础来说太大了(带数字的字符串)

标签 bash

看起来这是一个常见问题,但我没有看到适合我的特定场景的解决方案:

我的脚本接受了一些参数,这些参数是带有前导数字的字符串。然后脚本逐行循环输入流,查看用户的参数是否包含在输入流中。不幸的是,我的 if 语句收到错误,例如

...对于基数而言值太大(错误标记为“21000E1E06F54F”)...

#get user arguments
MyArray+=($OPTARG)

#for each array element, loop through an input looking for the string 
read <&3
if [[ $REPLY == *${MyArray[$i]}* ]]; then 
  found=1
fi

参数的一个例子是:21000E1E06F54F,但我不太愿意说它总是十六进制的——我宁愿把它当作一个字符串。显然,我可以在 $OPTARG 之前放一个字母,但随后字符串比较关闭,否则我需要添加代码来解决添加的字母。关于如何解决这个问题的任何想法?我希望我可以添加一些简单的语法来将变量转换为字符串...

谢谢,

最佳答案

问题是您正在遍历数组值,但随后尝试将数组 value 用作数组 index:

举个例子:

MyArray=(21000E1)
REPLY="FOO21000E1FOO"
for i in "${MyArray[@]}"
do
  if [[ $REPLY == *${MyArray[$i]}* ]]
  then
    echo "found"
  fi
done

这导致:

bash: 21000E1: value too great for base (error token is "21000E1")

以下是引用相同值的三种方式:

$i              # Correct, uses value directly
${MyArray[0]}   # Correct, looks up value using the value's index
${MyArray[$i]}  # WRONG,   tries to look up value using value as index

因为$i已经是值而不是索引,所以直接使用它:

MyArray=(21000E1)
REPLY="FOO21000E1FOO"
for i in "${MyArray[@]}"
do
  if [[ $REPLY == *"$i"* ]]
  then
    echo "found"
  fi
done

这导致 found

关于Bash:值对于基础来说太大了(带数字的字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651246/

相关文章:

bash - genisoimage 脚本不能刻录大于 4GB 但小于 4.5GB 的文件?

linux - BASH:可以在命令行上进行 grep,但不能在脚本中进行

用于文件夹列表的 Python Inotify

linux - BASH:如何对管道中的数字执行算术运算

linux - 在/var/www/site.access.log 中使用 tail -f

bash - 如何将多个命令的输出发送到单个 shell 管道?

linux - 关于ubuntu 11.04生成的.profile的问题

linux - 当文件名包含空格时使用 find 列出路径

linux - shell语言的数据组

linux - 如何在 CentOS 上不注销/登录的情况下刷新 session ?