Bash 脚本 - 在特定短语之后立即使用 Entry

发布于 2024-11-27 16:22:47 字数 468 浏览 0 评论 0原文

在我的项目 addgpg-apt (https://launchpad.net/addgpg-apt) 中,我希望能够将输入发送到程序中。根据该输入,说...
无法验证 PPA 中的签名。检查此问题是否已修复:NO_PUBKEY
...我如何使用基本的 Bash、grep 等从字符串中获取 并忽略字符串中的其他所有内容? (请注意,这些错误是由 apt-get/apt 生成的,因此字符串的结尾始终是 NO_PUBKEY

在 Java 中,这可以通过子字符串来完成,并根据短语 NO_PUBKEY 的位置仅获取 PGPkeyID,但我希望仅在 Bash 中完成此操作,因此任何解决方案将不胜感激。

In my project, addgpg-apt (https://launchpad.net/addgpg-apt), I'd like to be able to have input sent into the program. From that input, say...
Unable to verify signatures in PPA. Check that this is fixed: NO_PUBKEY <PGPkeyID>
... how can I, using basic Bash, grep, etc. get that <PGPkeyID> from the string and ignore everything else in the string? (Note that these errors are generated by apt-get/apt, and as such the end of the string is always NO_PUBKEY <PGPkeyID>)

In Java, this could be done with substring, and grab only that PGPkeyID based on the location of the phrase NO_PUBKEY, but I want this to be done in Bash only, so any solutions would be appreciated.

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

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

发布评论

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

评论(2

星光不落少年眉 2024-12-04 16:22:47

管道到 grep -o "NO_PUBKEY <.*>" | sed -e 's/.*<\(.*\)>.*/\1/'

将为您提供:

PGPkeyID


更新

假设您的输入如下:

The following signatures couldn't be verified because the public key is not available: NO_PUBKEY CAFE0123DEADBEEF
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 0123DEADBEEFCAFE
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY DEADBEEFCAFE0123
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY BEEFCAFE0123DEAD
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY CAFE0123DEADBEEF

以下命令将提取密钥:

grep -o 'PUBKEY [A-F0-9]\{16\}' | cut -f2 -d" " | sort -u

像这样:

0123DEADBEEFCAFE
BEEFCAFE0123DEAD
CAFE0123DEADBEEF
DEADBEEFCAFE0123

Piping to grep -o "NO_PUBKEY <.*>" | sed -e 's/.*<\(.*\)>.*/\1/'

will yield you:

PGPkeyID


Update

Assuming your input is like this:

The following signatures couldn't be verified because the public key is not available: NO_PUBKEY CAFE0123DEADBEEF
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 0123DEADBEEFCAFE
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY DEADBEEFCAFE0123
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY BEEFCAFE0123DEAD
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY CAFE0123DEADBEEF

The following command will extract the keys:

grep -o 'PUBKEY [A-F0-9]\{16\}' | cut -f2 -d" " | sort -u

Like this:

0123DEADBEEFCAFE
BEEFCAFE0123DEAD
CAFE0123DEADBEEF
DEADBEEFCAFE0123
伤痕我心 2024-12-04 16:22:47

您可以在纯 shell 中执行此操作,而无需生成进程:

cat << EOF > file
...
Unable to verify signatures in PPA. Check that this is fixed: NO_PUBKEY <PGPkeyID1>
...
Unable to verify signatures in PPA. Check that this is fixed: NO_PUBKEY <PGPkeyID2>
...
EOF

cat file | while read line; do
  if [[ $line == *\ NO_PUBKEY\ * ]]
  then
    echo ${line#* NO_PUBKEY }
  fi
done

You can do that in pure shell without spawning a process:

cat << EOF > file
...
Unable to verify signatures in PPA. Check that this is fixed: NO_PUBKEY <PGPkeyID1>
...
Unable to verify signatures in PPA. Check that this is fixed: NO_PUBKEY <PGPkeyID2>
...
EOF

cat file | while read line; do
  if [[ $line == *\ NO_PUBKEY\ * ]]
  then
    echo ${line#* NO_PUBKEY }
  fi
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文