【问题标题】:Bash Script with a loop of 2 variables带有 2 个变量的循环的 Bash 脚本
【发布时间】:2018-04-21 14:54:51
【问题描述】:

我很难解决这个问题。我需要向 GITLAP API 提供基于我拥有的文件创建的问题。通常文件的输出如下:

Microsoft xxx xxxxx - 远程代码执行 xxxxxx- 2018 年 4 月 xxxxx 更新 Red Hat Enterprise xxxx - java-1.8.0-xxxxx 多个 xxxxxxx- RHSA-xxxxxx

到目前为止一切顺利,我已经通过以下方式处理了这个问题:

while read in; do 
    curl --request POST --header "PRIVATE-TOKEN: xxxxxxxxxxxxx" https://gitlab.com/api/v3/projects/xxxxxx/issues?title="$in"; 
done < ~/input_file

现在的问题是我需要为此添加第二个变量,因为我需要介绍每个问题的描述,现在我的输入文件更改如下:

Microsoft 恶意软件保护 - 远程代码执行漏洞 - 2018 年 4 月安全更新 40697
Red Hat Enterprise Linux 6 - java-1.8.0-openjdk 多个漏洞 - RHSA-2018:1188 40861

我想构建这样的东西:

while read in; 
    curl --request POST --header "PRIVATE-TOKEN: xxxxxxxxxxx" "https://gitlab.com/api/v4/projects/xxxxx/issues?title=$in&description=https://myspecialink.com/portal/notifications/show/$id"; 
done < ~/input_file

例如:

  • $in: 必须是除我上面表示的粗体数字之外的所有内容。
  • $id: 只能是上面加粗的数字。

有人可以帮我指出实现这一目标的最佳方法吗?

【问题讨论】:

    标签: bash loops gitlab


    【解决方案1】:

    使用 bash parameter expansion 运算符来拆分输入。

    while read in; do
        id=${in##* }  # Remove everything up to last space
        in=${in% *}   # Remove everything from last space
        curl --request POST --header "PRIVATE-TOKEN: xxxxxxxxxxx" "https://gitlab.com/api/v4/projects/xxxxx/issues?title=$in&description=https://myspecialink.com/portal/notifications/show/$id"; 
    done
    

    【讨论】:

    • 谢谢你,我稍微调整了一下,因为我的输入文件不像我在这里给出的例子,但它确实是处理这个问题的好方法。谢谢你的帮助。
    猜你喜欢
    • 2013-09-17
    • 2019-07-20
    • 2011-10-25
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2016-02-01
    • 1970-01-01
    相关资源
    最近更新 更多