bash 基础值太大(错误标记为“0925”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5455779/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:41:54  来源:igfitidea点击:

Value too great for base (error token is "0925")

bashubuntuoctal

提问by oompahloompah

I have the following logic in my bash script:

我的 bash 脚本中有以下逻辑:

#!/bin/bash
local_time=$(date +%H%M)

if (( ( local_time > 1430  && local_time < 2230 ) || ( local_time > 0300 && local_time < 0430 ) )); then
 # do something
fi

Every now and then, I get the error specified in the title (any time above 08xxappears to trigger the error).

时不时地,我会收到标题中指定的错误(上面08xx出现的任何时间都会触发错误)。

Any suggestions on how to fix this?

对于如何解决这个问题,有任何的建议吗?

I am running on Ubuntu 10.04 LTS

我在 Ubuntu 10.04 LTS 上运行

[Edit]

[编辑]

I modified the script as suggested by SiegeX, and now, I am getting the error: [: 10#0910: integer expression expected.

我按照 SiegeX 的建议修改了脚本,现在出现错误:[: 10#0910: integer expression expected.

Any help?

有什么帮助吗?

回答by SiegeX

bashis treating your numbers as octalbecause of the leading zero

bash由于前导零,将您的数字视为八进制

From man bash

man bash

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Otherwise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 represent- ing the arithmetic base, and n is a number in that base. If base# is omitted, then base 10 is used.

带有前导 0 的常量被解释为八进制数。前导 0x 或 0X 表示十六进制。否则,数字采用 [base#]n 的形式,其中 base 是表示算术基数的 2 到 64 之间的十进制数,n 是该基数中的数字。如果省略 base#,则使用基数 10。

To fix it, specify the base-10 prefix

要修复它,请指定 base-10 前缀

#!/bin/bash
local_time="10#$(date +%H%M)"

if (( ( local_time > 1430  && local_time < 2230 ) || ( local_time > 0300 && local_time < 0430 ) )); then
 # do something
fi

回答by lambmj

Following the advice from this blog, this works:

按照这个博客的建议,这是有效的:

#!/bin/bash
local_time=`date +%H%M`
local_time="$(( 10#$local_time ))"

if (( ( local_time > 1430  && local_time < 2230 ) || ( local_time > 0300 && local_time < 0430 ) )); then
    echo "it is time!"
fi

回答by Serge Stroobandt

Solving the issue within the conditional test

解决条件测试中的问题

One may be forced to keep the variable as it is for a variety of reasons (e.g. file naming issues). If this is the case, solve the issue withinthe conditional test by explicitly specifying base10#:

由于各种原因(例如文件命名问题),人们可能被迫保持变量不变。如果是这种情况,解决问题的条件测试通过明确的基础10#

#!/bin/bash
local_time=$(date +%H%M)

if (( ( 10#${local_time} > 1430  && 10#${local_time} < 2230 ) || ( 10#${local_time} > 0300 && 10#${local_time} < 0430 ) )); then
 # do something
fi