Bash:增加字符串中的计数器变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43713362/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:06:32  来源:igfitidea点击:

Bash: Increment a counter variable within a string

bashshell

提问by Jonathan

In Bash, I'm trying to increment a counter variable (number) from within a text string. If I call the counter var alone it increments successfully, but If I echo the string variable on each iteration of my loop, the counter variable does not increment.

在 Bash 中,我试图从文本字符串中增加一个计数器变量(数字)。如果我单独调用计数器变量,它会成功增加,但是如果我在循环的每次迭代中回显字符串变量,计数器变量不会增加。

#!/bin/bash

number=1

yes="number$number/"

for i in 1 2 3
do
    echo $number

    echo $yes

    ((number++))

done

I get this output:

我得到这个输出:

1
number1/
2
number1/
3
number1/

Whereas I expect this:

而我希望这样:

1
number1/
2
number2/
3
number3/

I have also tried this:

我也试过这个:

yes="number${number}/"

..which gave the same incorrect result.

..这给出了同样的错误结果。

Thanks

谢谢

回答by Yu Jiaao

for i in 1 2 3
do
    echo $number
    yes="number$number/"
    echo $yes

    ((number++))

done

回答by ghoti

As you've been told in comments, expansion happens at the time of assignment. The variable $yescontains a string which includes the value of $numberat the time of assignment. After assignment, there is nothing in the content of $yeswhich would indicate any connection to the variable $number.

正如您在评论中被告知的那样,扩展发生在分配时。该变量$yes包含一个字符串,其中包含$number赋值时的值。赋值后,内容中没有任何内容$yes表明与变量有任何联系$number

There are two common ways to get this kind of functionality.

有两种常见的方法可以获得这种功能。

First, you can use eval.

首先,您可以使用eval.

#!/usr/bin/env bash

number=1

yes='number$number/'    # note the single quotes

for i in 1 2 3; do

    echo "$number"
    eval "echo \"$yes\""
    ((number++))

done

Note that the value of $yesis NOT being updated here -- it's simply being used to expand what is printed by echo.

请注意,$yes此处并未更新的值——它只是用于扩展echo.

You will find that many people discourage the use of eval, as it can have unintended security related consequences.

您会发现许多人不鼓励使用eval,因为它可能会产生与安全相关的意外后果。

Second, you could just update yeseach time you run through the loop.

其次,您可以在yes每次运行循环时进行更新。

#!/usr/bin/env bash

number=1

for i in 1 2 3; do

    echo "$number"

    yes="number$number/"
    echo "$yes"

    ((number++))

done

If you're looking to use this for formatting, then printfis your friend:

如果你想用它来格式化,那么printf你的朋友:

#!/usr/bin/env bash

number=1

yesfmt='number%d\n'

for i in 1 2 3; do

    echo "$number"
    printf "$yesfmt" "$number"
    ((number++))

done

Without knowing the bigger picture or what you're trying to achieve, it's difficult to recommend a strategy.

如果不知道更大的图景或您想要实现的目标,就很难推荐策略。

回答by rici

Use a function (I changed the name from yesto reportbecause yesis a standard posix utility, and anyway it wasn't at all descriptive.)

使用函数(我将名称从 更改为yesreport因为yes它是标准的 posix 实用程序,无论如何它根本没有描述性。)

#!/bin/bash
number=1
report() { echo "number$number/"; }
for i in 1 2 3; do
  echo $number
  report
  ((number++))
done