This question already has an answer here:
这个问题已经有了答案:
- Command not found error in Bash variable assignment 4 answers
- 命令未在Bash变量赋值4中找到错误
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.
反任务的引号是背景音。
I tried replacing COUNTER
with $COUNTER
like this:
我试着用$COUNTER来代替它:
$COUNTER = `expr $COUNTER + 1`
But that did not solve the problem and gave me the following error:
但这并没有解决问题,给了我以下的错误:
line7: 0: command not found.
4 个解决方案
#1
31
As @Cory rightly 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:
使用bash内置的“让”命令:
let COUNTER="COUNTER + 1"
-
or, using bash c-style expression:
或者,使用bash c-style表达式:
(( 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语句通常使用的是命令arg1 arg1……, bash将其解释为运行参数= 1的命令0。因此,错误消息:0:未找到命令。
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:没有找到命令。
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:
bash评估并最终解释为:
COUNTER=2
#2
6
Remove the spaces around the equals sign:
删除等号附近的空格:
COUNTER=`expr $COUNTER + 1`
#3
3
Another way.
另一种方式。
COUNTER=$(($COUNTER + 1))
#4
2
for i in {0..4}; do
echo "i will add this line to file mycreation" >> ./myfile
done
#1
31
As @Cory rightly 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:
使用bash内置的“让”命令:
let COUNTER="COUNTER + 1"
-
or, using bash c-style expression:
或者,使用bash c-style表达式:
(( 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语句通常使用的是命令arg1 arg1……, bash将其解释为运行参数= 1的命令0。因此,错误消息:0:未找到命令。
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:没有找到命令。
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:
bash评估并最终解释为:
COUNTER=2
#2
6
Remove the spaces around the equals sign:
删除等号附近的空格:
COUNTER=`expr $COUNTER + 1`
#3
3
Another way.
另一种方式。
COUNTER=$(($COUNTER + 1))
#4
2
for i in {0..4}; do
echo "i will add this line to file mycreation" >> ./myfile
done