如何在 bash 中使用 mod 运算符?
我正在尝试这样的行:
for i in {1..600}; do wget http://example.com/search/link $i % 5; done;
我想要得到的输出是:
wget http://example.com/search/link0
wget http://example.com/search/link1
wget http://example.com/search/link2
wget http://example.com/search/link3
wget http://example.com/search/link4
wget http://example.com/search/link0
但我实际得到的是:
wget http://example.com/search/link
I'm trying a line like this:
for i in {1..600}; do wget http://example.com/search/link $i % 5; done;
What I'm trying to get as output is:
wget http://example.com/search/link0
wget http://example.com/search/link1
wget http://example.com/search/link2
wget http://example.com/search/link3
wget http://example.com/search/link4
wget http://example.com/search/link0
But what I'm actually getting is just:
wget http://example.com/search/link
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试以下操作:
$(( ))
语法执行 内容的算术评估。Try the following:
The
$(( ))
syntax does an arithmetic evaluation of the contents.您必须将数学表达式放在 $(( )) 内。
一行:
多行:
You must put your mathematical expressions inside $(( )).
One-liner:
Multiple lines:
这可能是题外话。但是对于 for 循环中的 wget ,你当然可以这样做
This might be off-topic. But for the wget in for loop, you can certainly do
bash 中的数学:如何使用所有 bash 运算符(
+
、-
、/
、*
、**
、&
、&&
、<<
等),以及算术扩展,在 到目前为止,在这个问题的 346,000 名访客中,我敢打赌,其中 344,900 名访客只是想回答这个问题的标题
Math in bash: how to use all bash operators (
+
,-
,/
,*
,**
,&
,&&
,<<
, etc.), and arithmetic expansion, in bashOf the 346k visitors to this question thus far, I'd be willing to bet 344.9k of them just want the title of this question answered ????:
Even I googled "bash modulus" looking for that answer, and landed here. So, now that I've figured it out, let's just jump straight to it:
How to use the modulus (
%
) operator in bashJust do this, for instance:
Example with variables:
Store the result into a variable:
The main links to study for these concepts are these, from the official GNU bash user manual:
More on bash "arithmetic expansion"
I learned the above from @Mark Longair's answer (although it took me some effort to comprehend it all), and that's where I got the link just below. I then did more research.
The
$(( ))
part is called "Arithmetic Expansion", and is described in the official GNUbash
user manual here: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Arithmetic-Expansion.Basic examples (place
echo
in front of each one to see the result print to the screen):Double quotes around the arithmetic expansion are not needed. From the manual above (emphasis added):
For all shell arithmetic operators, see the "Shell Arithmetic" section of the GNU bash manual here: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Arithmetic
You essentially have all of the C language mathematical operators at your disposal. The arithmetic is done "in fixed-width integers with no check for overflow", so if you do
echo $((11/10))
orecho $((19/10))
you'll get1
in both cases since the fractional part is truncated for integers.From the manual link just above (emphasis added):
Since the arithmetic operators in bash have the same precedence as in C, as it states above, you can also reference the C Operator Precedence from the cppreference community wiki here: https://en.cppreference.com/w/c/language/operator_precedence <-- put that in your toolbag.
Shell Arithmetic: here are all of the supported operators from the GNU Bash manual
They are listed in order of highest to lowest precedence:
Using alternate bases in your arithmetic, such as binary (base-2), octal (base-8), and hex (base-16)
To learn about using different bases, such as base-2 (binary), base-8 (octal) or base-16 (hex) with the bash arithmetic operators, read the next couple paragraphs below the "Shell Arithmetic" list above in the manual.
Here are a few quick examples with input numbers which are decimal (base-10), octal (base-8), hex (base-16), and binary (base-2), used in the math:
To print as hex, use
printf "0x%X\n" number
:To print as binary, use
bc
(see my answer here):See also
这篇文章相当老了,但我想我会做出贡献,因为我在尝试研究通过自动化设置键盘颜色的相同问题时偶然发现了它。
我创建了一个简单的 BASH 脚本,每分钟从我的 ROOT chrontab 调用该脚本,以随着时间的推移设置键盘颜色。您可以调整颜色模式和模数以满足您的需求。这只是一个好的起点。
希望你喜欢它。
This post is rather old but I thought I would contribute since I stumbled upon it while trying to research the same issue of setting keyboard color through automation.
I created a simple BASH script that I call from my ROOT chrontab every minute to set the keyboard color as the day progresses. You can tweak the color patterns and the modulo to match your needs. This is just a good starting point.
Hope you like it.