Bash:如何仅更改一个参数值并保持其他参数值保持不变

发布于 2025-01-18 07:01:01 字数 494 浏览 1 评论 0原文

我有一个bash脚本,该脚本在yaml文件中编辑两个标签,这些值作为参数传递。我如何在运行脚本时只能更新一个标签

#!/bin/bash

#Update UI-ImageTag
sed -i -e '/APP:/{n;n;s/\(imageTag\).*/\1: "'"app-ui-$1"'" /}' \
       -e '/APP:/{n;n;n;n;s/\(imageTag\).*/\1: "'"app-db-$2"'" /}' \
       values.yaml

并传递参数$ 1和$ 2 ex(./ script.sh.sh 1.0 2.0)的方案,其中两个标签都在values.yaml文件中更新了两个标签。 ,但是当我只给一个参数的值并将另一个参数留为空时(即,仅通过$ 1传递值来执行脚本),则value.yaml文件中的$ 2标签被一个空值替换。 如何更改脚本,以便在我不需要更改app-db的标签的情况下,如果我不以$ 2的价格传递一个值,它会使yaml文件中的旧值保持不变

I have a bash script that edits two tags in a yaml file and these values are passed as parameters. How can I change the script for a scenario where only one tag has to be updated

#!/bin/bash

#Update UI-ImageTag
sed -i -e '/APP:/{n;n;s/\(imageTag\).*/\1: "'"app-ui-$1"'" /}' \
       -e '/APP:/{n;n;n;n;s/\(imageTag\).*/\1: "'"app-db-$2"'" /}' \
       values.yaml

While running the script and passing values for parameters $1 and $2 Ex(./script.sh 1.0 2.0) both the tags are updated in the values.yaml file, but when I give value for only one parameter and leave the other one empty(i.e., execute the script by passing value for $1 only), then the $2 tag in values.yaml file is replaced with an empty value.
How to change the script so that in a scenario where I dont need to change the tag of app-db and if I dont pass a value for $2, it keeps the old value in the yaml file unchanged

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2025-01-25 07:01:01

您可以要求 shell 在未设置某些内容时提供默认值,只需更改 sed 脚本以捕获旧值并替换为旧值,这样您实际上就不会更改任何内容。

#!/bin/bash
sed -i -e '/APP:/{n;n;s/\(imageTag\).*/\1: "'"app-ui-$1"'" /}' \
       -e '/APP:/{n;n;n;n;s/\(imageTag\)\(.*\)/\1: "'"${2+app-db-}${2-\\2}"'" /}' \
       values.yaml

仅当设置了 $2${2-\\2 时,参数扩展 ${2+value} 才会扩展为 value } 在未设置时扩展为 \2,在设置时扩展为其值。您会注意到,为此目的,正则表达式也略有更改,以将 imageTag 之后的文本捕获到 \2 中。

You can ask the shell to supply a default value when something is unset, and just change your sed script to capture the old value and replace with that, so that you effectively don't change anything.

#!/bin/bash
sed -i -e '/APP:/{n;n;s/\(imageTag\).*/\1: "'"app-ui-$1"'" /}' \
       -e '/APP:/{n;n;n;n;s/\(imageTag\)\(.*\)/\1: "'"${2+app-db-}${2-\\2}"'" /}' \
       values.yaml

The parameter expansion ${2+value} expands to value only if $2 is set, and ${2-\\2} expands to \2 when it is unset, and to its value when it's set. You'll notice that the regex was also changed slightly to capture the text after imageTag into \2 for this purpose.

女中豪杰 2025-01-25 07:01:01

建议尝试awk脚本:

awk '
/APP:/{skip2 = 3; skip4 = 5}
!skip2 && inp1 {$0 = gensub("(imageTag)","\\1: \"app-ui-"inp1"\" \\\\",1)}
!skip4 && inp2 {$0 = gensub("(imageTag)","\\1: \"app-db-"inp2"\" \\\\",1)}
{print; --skip2; --skip4}
' inp1="$1" inp2="$2" values.yaml > values.yaml.1
mv values.yaml.1 values.yaml

未测试,未提供示例数据。

解释:

awk ' # start awk script
/APP:/ { # for evey line matching RegExp /APP:/ set down counter to skipped line
  skip2=3; # set skip2 action to next 2 lines including this is 3
  skip4=5; # set skip4 action to next 4 lines including this is 5
}
skip2 == 0 && inp1 { # in current line, if skip2 reached 0, and variable inp1 exist
  $0 = gensub("(imageTag)","\\1: \"app-ui-"inp1"\" \\\\",1); # do string subtition using gensub functin on current line.
}
skip4 == 0  && inp2 { # in current line, if skip4 reached 0, and variable inp2 exist
  $0 = gensub("(imageTag)","\\1: \"app-db-"inp2"\" \\\\",1); # do string subtition using gensub functin on current line.
}
{  # on every line
  print $0; # print current line
  --skip2;  # decrement skip2
  --skip4;  # decrement skip4
}' \  # end awk script
inp1="$1" \  # assign 1st script argument to awk variable inp1
inp2="$2" \  # assign 2nd script argument to awk variable inp1
values.yaml > values.yaml.1 # redirect output to values.yaml.1
mv values.yaml.1 values.yaml # override output to values.yaml

Suggesting try awk script:

awk '
/APP:/{skip2 = 3; skip4 = 5}
!skip2 && inp1 {$0 = gensub("(imageTag)","\\1: \"app-ui-"inp1"\" \\\\",1)}
!skip4 && inp2 {$0 = gensub("(imageTag)","\\1: \"app-db-"inp2"\" \\\\",1)}
{print; --skip2; --skip4}
' inp1="$1" inp2="$2" values.yaml > values.yaml.1
mv values.yaml.1 values.yaml

Not tested, not provided sample data.

Explanation:

awk ' # start awk script
/APP:/ { # for evey line matching RegExp /APP:/ set down counter to skipped line
  skip2=3; # set skip2 action to next 2 lines including this is 3
  skip4=5; # set skip4 action to next 4 lines including this is 5
}
skip2 == 0 && inp1 { # in current line, if skip2 reached 0, and variable inp1 exist
  $0 = gensub("(imageTag)","\\1: \"app-ui-"inp1"\" \\\\",1); # do string subtition using gensub functin on current line.
}
skip4 == 0  && inp2 { # in current line, if skip4 reached 0, and variable inp2 exist
  $0 = gensub("(imageTag)","\\1: \"app-db-"inp2"\" \\\\",1); # do string subtition using gensub functin on current line.
}
{  # on every line
  print $0; # print current line
  --skip2;  # decrement skip2
  --skip4;  # decrement skip4
}' \  # end awk script
inp1="$1" \  # assign 1st script argument to awk variable inp1
inp2="$2" \  # assign 2nd script argument to awk variable inp1
values.yaml > values.yaml.1 # redirect output to values.yaml.1
mv values.yaml.1 values.yaml # override output to values.yaml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文