是否可以从 gpg 中 grep 这个输出

是否可以从 gpg 中 grep 这个输出

采取以下命令(真实示例):

~$ gpg --edit-key [email protected] showpref quit
(...)
[ultimate] (1). Foo Bar <[email protected]>
     Cipher: AES256, AES192, AES, CAST5, 3DES
     AEAD: 
     Digest: SHA256, SHA1, SHA384, SHA512, SHA224
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
     Features: MDC, AEAD, Keyserver no-modify
     Preferred keyserver: ldap://keyserver.pgp.com
~$

将输出通过管道输送到 a 中| grep不起作用:

~$ gpg --edit-key [email protected] showpref quit | grep Compression
(...)
[ultimate] (1). Foo Bar <[email protected]>
     Cipher: AES256, AES192, AES, CAST5, 3DES
     AEAD: 
     Digest: SHA256, SHA1, SHA384, SHA512, SHA224
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
     Features: MDC, AEAD, Keyserver no-modify
     Preferred keyserver: ldap://keyserver.pgp.com
~$

有办法让它发挥作用吗?例如,我想得到的结果是:

~$ gpg --edit-key [email protected] showpref quit | grep Compression
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
~$

[编辑:到目前为止我尝试过的]:

@steeldriver,@RomeoNinov:我认为重定向不会stderr解决问题。看起来 的输出并没有gpg转到stderr.

基本上,gpg是一个交互式命令,但启动gpg ... cmd1 cmd2使其成为非交互式命令(例如,与在's shell中gpg ... showpref quit交互式执行相同)。showprefquitgpg

@钢铁司机:

~$ gpg --edit-key [email protected] showpref quit 2> >(grep Compression)
(...)
[ultimate] (1). Foo Bar <[email protected]>
     Cipher: AES256, AES192, AES, CAST5, 3DES
     AEAD: 
     Digest: SHA256, SHA1, SHA384, SHA512, SHA224
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
     Features: MDC, AEAD, Keyserver no-modify
     Preferred keyserver: ldap://keyserver.pgp.com
~$

@罗密欧尼诺夫:

~$ gpg --edit-key [email protected] showpref quit 2>&1| grep Compression
(...)
[ultimate] (1). Foo Bar <[email protected]>
     Cipher: AES256, AES192, AES, CAST5, 3DES
     AEAD: 
     Digest: SHA256, SHA1, SHA384, SHA512, SHA224
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
     Features: MDC, AEAD, Keyserver no-modify
     Preferred keyserver: ldap://keyserver.pgp.com
~$

答案1

解决方案是使用--batch.这将有助于gpg将信息发送到标准文件处理程序。

# gpg  --batch --edit-key [email protected] showpref quit 2>&1 |grep Com
     Compression: ZLIB, BZIP2, ZIP, Uncompressed

相关内容