为什么此bash expr命令不起作用? [英] Why doesn't this bash expr command work?

查看:502
本文介绍了为什么此bash expr命令不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在bash脚本中增加变量,但它不起作用.这是我的代码:

I am trying to increment a variable in a bash script and it is not working. Here is my code:

#! /bin/bash

COUNTER=0
while [ $COUNTER -lt 5 ]
do
    echo "i will add this line to file mycreation">>./myfile
    COUNTER = `expr $COUNTER + 1`
done

COUNTER赋值周围的引号是反引号.

The quotes around the COUNTER assignment are backticks.

我尝试用$COUNTER替换COUNTER,如下所示:

I tried replacing COUNTER with $COUNTER like this:

$COUNTER = `expr $COUNTER + 1`

但这并不能解决问题,并给了我以下错误:

But that did not solve the problem and gave me the following error:

line7: 0: command not found. 

推荐答案

正如 @Cory 正确指出的那样,等号前后不能有空格,否则bash会使COUNTER混淆.

As @Cory rightly pointed out, there should not be spaces around the equal sign or else bash will confuse COUNTER for a command.

COUNTER=$(expr $COUNTER + 1)

走出话题...

也就是说,通过使用以下替代方法,可以避免让bash派生一个子进程:

going off-topic ...

That said, you could avoid having bash fork a subprocess by using the following alternatives:

let COUNTER="COUNTER + 1"

  • ,或者使用bash c样式表达式:

  • or, using bash c-style expression:

    (( COUNTER++ ))
    

  • 实际上,您的while循环可以写为:

    In fact, your while loop can be written as:

    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 
    

    我的猜测是您在第7行上的状态

    my guess is what you had on line 7 was

    $COUNTER = `expr $COUNTER + 1`
    --------   --------------------
        |                 |
    Evaluated to 0        |
                      Evaluated to 1
    

    bash最终看到的是0 = 1,并且由于bash语句通常采用command arg1 arg1 ...形式,因此bash将其解释为运行带有参数= 1的命令0.因此,错误消息:0: command not found.

    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最终解释为:

    When you removed the spaces around the equal sign, what bash ends up interpreting is:

    0=1
    

    表示没有任何参数的运行命令0=1,因此错误为0=1: command not found.

    which means run command 0=1 with no arguments, hence the error 0=1: command not found.

    变量分配的格式应为VAR_NAME=VALUE(无$),因此您应使用的语法为:

    Variable assignments should be in the form VAR_NAME=VALUE (without the $), so the syntax you should be using is:

    COUNTER=`expr $COUNTER + 1` # or any of the variants above
    

    哪个bash评估并最终解释为:

    which bash evaluates and eventually interpret as:

    COUNTER=2
    

    这篇关于为什么此bash expr命令不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆