用 bash 递增

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

Increment with bash

bashshellincrement

提问by D????V????

I'm stuck trying to increment a variable in an .xml file. The tag may be in a file 100 times or just twice. I am trying to add a value that will increment the amount several times. I have included some sample code I am working on, but when I run the script it will only place a one and not increment further. Advice would be great on what I'm doing wrong.

我一直在尝试增加 .xml 文件中的变量。标签可能在文件中出现 100 次或仅两次。我正在尝试添加一个可以多次增加数量的值。我已经包含了一些我正在处理的示例代码,但是当我运行脚本时,它只会放置一个并且不会进一步增加。关于我做错了什么的建议会很好。

for xmlfile in $(find $DIRECTORY -type f -name \*.xml); do
  TFILE="/tmp/$directoryname.$$"
  FROM='><process>'
  TO=' value\=""><process>'
  i=0
  while [ $i -lt 10 ]; do
    i=`expr $i + 1`
    FROM='value\=""'
    TO='value\="'$i'"'
  done
  sed "s/$FROM/$TO/g" "$xmlfile" > $TFILE && mv $TFILE "$xmlfile"
done

The whileloop was something I just placed to test the code. It will insert the <process>but it will not insert the increment.

while回路是我刚才放置测试代码。它将插入<process>但不会插入增量。

My end goal:

我的最终目标:

<process>value="1"</process>
<process>value="2"</process>
<process>value="3"</process>
<process>value="4"</process>

And so on as long as <process>is present in the file it needs to increment.

依此类推,只要<process>文件中存在它就需要递增。

采纳答案by Gilles Quenot

For a proper increment in bash, use a for loop (C style) :

要在 中适当增加bash,请使用 for 循环(C 风格):

n=10
for ((i=1; i<=n; i++)) {
    printf '<process>value="%d"</process>\n' $i
}

OUTPUT

输出

<process>value="1"</process>
<process>value="2"</process>
<process>value="3"</process>
<process>value="4"</process>
<process>value="5"</process>
<process>value="6"</process>
<process>value="7"</process>
<process>value="8"</process>
<process>value="9"</process>
<process>value="10"</process>

NOTE

笔记

expris a program used in ancient shell code to do math. In Posix shells like bash, use $(( expression )). In bash and ksh93, you can also use (( expression ))or let expressionif you don't need to use the result in an expansion.

expr是一个在古代 shell 代码中用来做数学的程序。在像 bash 这样的 Posix shell 中,使用 $(( 表达式 ))。在 bash 和 ksh93 中,您也可以使用(( expression ))orlet expression如果您不需要在扩展中使用结果。

EDIT

编辑

If I misunderstood your needs and you have a file with blank values like this :

如果我误解了您的需求并且您有一个包含如下空白值的文件:

<process>value=""</process>

try this :

尝试这个 :

$ perl -i -pe '$c++; s/<process>value=""/<process>value"$c"/g' file.xml
<process>value"1"</process>
<process>value"2"</process>
<process>value"3"</process>
<process>value"4"</process>
<process>value"5"</process>
<process>value"6"</process>
<process>value"7"</process>

-iswitch edit the file for real, so take care.

-iswitch 真正编辑文件,所以要小心。

回答by sampson-chen

I just tested your code and it seems to correctly increment i.

我刚刚测试了您的代码,它似乎正确递增i

You could try changing your increment syntax from:

您可以尝试从以下位置更改增量语法:

i=`expr $i + 1`

To

i=$((i+1))

回答by Cory Klein

This is the simplest way to increment a variable in bash:

这是在 bash 中增加变量的最简单方法:

i=0
((i++))

回答by dskrad

This also works.

这也有效。

Declaring the variable as an integer.

将变量声明为整数。

declare -i i=0

Then later you can increment like so:

然后你可以像这样递增:

i+=1

回答by William Pursell

Use awk:

使用awk

awk '{gsub( "value=\"\"", "value=" i++ ); print }' i=1 input-file

This will replace the string value=""with value="1", value="2", etc. You can easily change the start value and the increment ( eg ..."value=" i ); i+=5; print)

这将替换串value=""value="1"value="2"等您可以轻松更改起始值和增量(例如..."value=" i ); i+=5; print