简体   繁体   English

使用 SED/AWK 增加 PKGBUILD 文件中变量的值

[英]Use SED/AWK to increment value of a variable in a PKGBUILD file

I have a PKGBUILD file (for AUR) which I am maintaining.我有一个正在维护的 PKGBUILD 文件(用于 AUR)。 It has a value 'pkgrel' which I increment by 1 (manually), before building the package each time.它有一个值“pkgrel”,在每次构建 package 之前,我将其递增 1(手动)。

To summarize, if I want to build the package the sixth time, I'll manually set the value as总而言之,如果我想第六次构建 package,我将手动将值设置为

pkgrel=6

There are other processes for which I have written a bash script.还有其他进程,我为此编写了 bash 脚本。 I am stuck here.我被困在这里。 Please guide me on how to increment that value of pkgrel via any sed/awk expression.请指导我如何通过任何 sed/awk 表达式增加 pkgrel 的值。 I want it that way so that say, I build the package the 7th time, the SED/AWK expression should set the value to 7我想要这样说,我第 7 次构建 package,SED/AWK 表达式应该将值设置为 7

pkgrel=7

before I run the makepkg command in my script.在我的脚本中运行 makepkg 命令之前。

I am not restricting answers to sed/awk, if any other method works, please do guide me.我不限制对 sed/awk 的回答,如果任何其他方法有效,请指导我。 The only condition is it should be bash.唯一的条件是它应该是 bash。

Assuming pkgrel is the first word in its line and there are no spaces around it or around the = , you can use the following awk command to print an updated PKGBUILD.假设pkgrel是其行中的第一个单词,并且它周围或=周围没有空格,您可以使用以下awk命令打印更新的 PKGBUILD。

awk -F= -v OFS== '$1=="pkgrel" {$2++} 1' PKGBUILD 

To update the file in-place you can use GNU awk's -i inplace option.要就地更新文件,您可以使用 GNU awk 的-i inplace选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM