bash 在bash脚本中将字符串转换为整数 - “前导零”数字错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12821715/
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
Convert string into integer in bash script - "Leading Zero" number error
提问by RobertoFRey
In a text file, test.txt, I have the next information:
在文本文件 test.txt 中,我有以下信息:
sl-gs5 desconnected Wed Oct 10 08:00:01 EDT 2012 1001
I want to extract the hour of the event by the next command line:
我想通过下一个命令行提取事件的小时数:
hour=$(grep -n sl-gs5 test.txt | tail -1 | cut -d' ' -f6 | awk -F ":" '{print }')
and I got "08". When I try to add 1,
我得到了“08”。当我尝试添加 1 时,
14 echo $((hour+1))
I receive the next error message:
我收到下一条错误消息:
./test2.sh: line 14: 08: value too great for base (error token is "08")
If variables in Bash are untyped, why?
如果 Bash 中的变量是无类型的,为什么?
回答by choroba
See ARITHMETIC EVALUATIONin man bash
:
参见算术求于man bash
:
Constants with a leading 0 are interpreted as octal numbers.
带有前导 0 的常量被解释为八进制数。
You can remove the leading zero by parameter expansion:
您可以通过参数扩展删除前导零:
hour=${hour#0}
or force base-10 interpretation:
或强制 base-10 解释:
$((10#$hour + 1))
回答by shellter
what I'd call a hack, but given that you're only processing hour values, you can do
我称之为 hack,但鉴于您只处理小时值,您可以这样做
hour=08
echo $(( ${hour#0} +1 ))
9
hour=10
echo $(( ${hour#0} +1))
11
with little risk.
风险很小。
IHTH.
IHTH。
回答by makim
You could also use bc
你也可以使用 bc
hour=8
result=$(echo "$hour + 1" | bc)
echo $result
9
回答by jbrahy
Here's an easy way, albeit not the prettiest way to get an int value for a string.
这是一种简单的方法,尽管不是获取字符串 int 值的最佳方法。
hour=`expr $hour + 0`
Example
例子
bash-3.2$ hour="08"
bash-3.2$ hour=`expr $hour + 0`
bash-3.2$ echo $hour
8
回答by Feng Xue
How about sed?
sed呢?
hour=`echo $hour|sed -e "s/^0*//g"`
回答by user unknown
Since hours are always positive, and always 2 digits, you can set a 1 in front of it and subtract 100:
由于小时数始终为正数且始终为 2 位数,因此您可以在其前面设置 1 并减去 100:
echo $((1$hour+1-100))
which is equivalent to
这相当于
echo $((1$hour-99))
Be sure to comment such gymnastics. :)
一定要评论这样的体操。:)
回答by avivamg
In Short:In order to deal with "Leading Zero" numbers (any 0 digit that comes before the first non-zero) in bash
- Use bc
An arbitrary precision calculator language
简而言之:为了处理bash 中的“前导零”数字(第一个非零之前的任何 0 位)-使用bc
任意精度计算器语言
Example:
例子:
a="000001"
b=$(echo $a | bc)
echo $b
Output: 1
输出:1
From Bash manual:
来自 Bash 手册:
"bc is a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities in the syntax to the C programming lan- guage. A standard math library is available by command line option. If requested, the math library is defined before processing any files. bc starts by processing code from all the files listed on the command line in the order listed. After all files have been processed, bc reads from the standard input. All code is executed as it is read. (If a file contains a command to halt the processor, bc will never read from the standard input.)"
“bc 是一种支持任意精度数和语句交互执行的语言。在语法上与 C 编程语言有一些相似之处。通过命令行选项可以使用标准数学库。如果需要,可以定义数学库在处理任何文件之前。bc 首先按照列出的顺序处理命令行中列出的所有文件中的代码。处理完所有文件后,bc 从标准输入中读取。所有代码都在读取时执行。(如果 a文件包含停止处理器的命令,bc 永远不会从标准输入中读取。)”
回答by Charles Addis
The leading 0 is leading to bash trying to interpret your number as an octal number, but octal numbers are 0-7, and 8 is thus an invalid token.
前导 0 导致 bash 试图将您的数字解释为八进制数,但八进制数是 0-7,因此 8 是无效标记。
If I were you, I would add some logic to remove a leading 0, add one, and re-add the leading 0 if the result is < 10.
如果我是你,我会添加一些逻辑来删除前导 0,添加 1,如果结果 < 10,则重新添加前导 0。