Linux 为什么这个 bash expr 命令不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4785068/
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
Why doesn't this bash expr command work?
提问by Marin
I am trying to increment a variable in a bash script and it is not working. Here is my code:
我正在尝试在 bash 脚本中增加一个变量,但它不起作用。这是我的代码:
#! /bin/bash
COUNTER=0
while [ $COUNTER -lt 5 ]
do
echo "i will add this line to file mycreation">>./myfile
COUNTER = `expr $COUNTER + 1`
done
The quotes around the COUNTER
assignment are backticks.
COUNTER
作业周围的引号是反引号。
I tried replacing COUNTER
with $COUNTER
like this:
我试图取代COUNTER
以$COUNTER
这样的:
$COUNTER = `expr $COUNTER + 1`
But that did not solve the problem and gave me the following error:
但这并没有解决问题,并给了我以下错误:
line7: 0: command not found.
采纳答案by Shawn Chin
As @Coryrightly pointed out, there should not be spaces around the equal sign or else bash will confuse COUNTER
for a command.
正如@Cory正确指出的那样,等号周围不应有空格,否则 bash 会混淆COUNTER
命令。
COUNTER=$(expr $COUNTER + 1)
going off-topic ...
跑题了……
That said, you could avoid having bash fork a subprocess by using the following alternatives:
也就是说,您可以通过使用以下替代方法来避免 bash fork 子进程:
Using the bash builtin 'let' command:
let COUNTER="COUNTER + 1"
or, using bash c-style expression:
(( COUNTER++ ))
let COUNTER="COUNTER + 1"
或者,使用 bash c 风格的表达式:
(( COUNTER++ ))
In fact, your while loop can be written as:
事实上,你的 while 循环可以写成:
for ((COUNTER=0; COUNTER <= 5 ; COUNTER++))
do
echo "i will add this line to file mycreation">>./myfile
done
Breaking down the error message
分解错误信息
When you were met with the error:
当您遇到错误时:
line 7: 0: command not found.
'-----' '--' '------------------'
| | |
location | Description of error.
culprit
my guess is what you had on line 7 was
我猜你在第 7 行的内容是
$COUNTER = `expr $COUNTER + 1`
-------- --------------------
| |
Evaluated to 0 |
Evaluated to 1
What bash ends up see is 0 = 1
and since bash statements are generally in the form command arg1 arg1 ...
, bash interprets it as run the command 0
with arguments = 1
. Thus the error message : 0: command not found
.
bash 最终看到的是0 = 1
,由于 bash 语句通常采用这种形式command arg1 arg1 ...
,因此 bash 将其解释为运行0
带参数的命令= 1
。因此错误信息:0: command not found
。
When you removed the spaces around the equal sign, what bash ends up interpreting is:
当您删除等号周围的空格时,bash 最终解释的是:
0=1
which means run command 0=1
with no arguments, hence the error 0=1: command not found
.
这意味着0=1
不带参数运行命令,因此错误0=1: command not found
.
Variable assignments should be in the form VAR_NAME=VALUE
(without the $
), so the syntax you should be using is:
变量赋值的形式应该是VAR_NAME=VALUE
(不带$
),所以你应该使用的语法是:
COUNTER=`expr $COUNTER + 1` # or any of the variants above
which bash evaluates and eventually interpret as:
which bash 评估并最终解释为:
COUNTER=2
回答by Cory Klein
Remove the spaces around the equals sign:
删除等号周围的空格:
COUNTER=`expr $COUNTER + 1`
回答by webbi
Another way.
其它的办法。
COUNTER=$(($COUNTER + 1))
回答by August Karlstrom
for i in {0..4}; do
echo "i will add this line to file mycreation" >> ./myfile
done