Linux shell中的整数加法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8723987/
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
Integer addition in shell
提问by Jaseem
Here is my simple shell code. I want the result to be 2.Shell treats everything as a string. How can i get this done ?
这是我的简单shell代码。我希望结果为 2.Shell 将所有内容视为字符串。我怎样才能做到这一点?
num=1
num=$(( $num + 1 ))
EDIT :
编辑 :
Complete code : Whats wrong in this if i want to print from 1 to 10 ?
完整代码:如果我想从 1 打印到 10,这有什么问题?
#! /bin/bash
num=1
until test $num -eq 10
do
num=$(( $num + 1 ))
echo $num
done
回答by NPE
In bash
, you don't need to do anything special:
在 中bash
,你不需要做任何特别的事情:
aix@aix:~$ num=1
aix@aix:~$ num=$(( $num + 1 ))
aix@aix:~$ echo $num
2
回答by Fredrik Pihl
works for me
为我工作
$ num=1
$ num=$(( $num + 1 ))
$ echo $num
2
What output do you get?
你得到什么输出?
Read more about bash Arithmetic @ tldp
阅读更多关于 bash Arithmetic @ tldp
EDIT
编辑
To do something 10 times in bash you can use (using brace-expansion}
要在 bash 中执行 10 次操作,您可以使用(使用括号扩展}
$ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
However, you cannot use variables between the {}
. If this is the case, use seq
instead.
但是,您不能在{}
. 如果是这种情况,请seq
改用。
回答by SiegeX
You just did:
你刚刚做了:
$ num=1; num=$(( $num + 1 ));echo $num
2
Note: You don't need to quote variables inside $(( ))
. Also, you can just use $((num++))
注意:您不需要在$(( ))
. 此外,你可以只使用$((num++))
回答by Hemant Metalia
try this
尝试这个
$ num=1; num=`expr $num + 1`; echo $num;
回答by dogbane
Use ((num++))
as shorthand for incrementing num
.
使用((num++))
速记递增num
。
$ num=1
$ ((num++))
$ echo $num
2
回答by tonio
You can use something like:
您可以使用以下内容:
num=1
num=`expr $num + 1`
It will also work with non bash shells, like ksh.
它也适用于非 bash shell,如 ksh。
回答by totaam
You are not specifying which shell you are using, but the most concise form I know is this one (works at least in bash):
您没有指定您使用的是哪个 shell,但我知道的最简洁的形式是这个(至少在 bash 中有效):
num=$[num+1]
If only incrementing by one and changing the variable itself rather than printing/assigning, then:
如果仅增加 1 并更改变量本身而不是打印/分配,则:
((num++))
Is a better/more elegant solution. See dogbane's answer for that.
是更好/更优雅的解决方案。请参阅 dogbane 的答案。
If looping over the values, I would use this form instead:
如果循环遍历这些值,我会改用这种形式:
for i in `seq 1 10`; do
echo $i
done
回答by Juha Laiho
@tonio; please don't advocate using subshell (` ... or $( ... ) ) constructs when they're not needed (to keep confusion to the maximum, $(( ... )) is not a sub-shell construct). Sub-shells can make a tremendous performance hit even with rather trivial amounts of data. The same is true for every place where an external program is used to do somethign that could be done with a shel built-in.
@托尼奥;请不要提倡在不需要时使用 subshell (` ... 或 $( ... ) ) 构造(为了最大程度地保持混乱,$(( ... )) 不是 sub-shell 构造)。即使使用相当微不足道的数据量,子 shell 也会对性能产生巨大的影响。对于使用外部程序来做一些可以用内置的 Shel 完成的事情的每个地方都是如此。
Example:
例子:
num=1
time while [[ $num -lt 10000 ]]; do
num=$(( num+1 ))
done
echo $num
num=1
time while /bin/test $num -lt 10000; do
num=$( /bin/expr $num + 1 )
done
echo $num
Output (run in ksh on Linux):
输出(在 Linux 上的 ksh 中运行):
real 0m0.04s user 0m0.04s sys 0m0.01s 10000 real 0m20.32s user 0m2.23s sys 0m2.92s 10000
...so run-time factor of 250, and CPU-time factor of 100. I admit the example I used was a exaggerated one, with explicitly requiring all built-ins to be bypassed, but I think the point was made: creating new processes is expenisve, avoid it when you can, and know your shell to recognise where new processes are created.
...所以运行时间系数为 250,CPU 时间系数为 100。我承认我使用的示例被夸大了,明确要求绕过所有内置函数,但我认为重点是:创建新进程是昂贵的,尽可能避免它,并了解您的 shell 以识别新进程的创建位置。
回答by potong
This might work for you:
这可能对你有用:
num=1; ((num++)); echo $num
2
or
或者
num=1; echo $((++num))
2
for loops
for 循环
for num in {1..10}; do echo $num; done
or (in bash at least)
或(至少在 bash 中)
for ((num=1; num<=10; num++)) { echo $num; }
second loop more useful when more programming involved:
当涉及更多编程时,第二个循环更有用:
for (( num=1,mun=10; num<=10; num++,mun--)) { echo $num $mun; }