如何在 Solaris 11 上以机器可读格式获取 pkg 属性

如何在 Solaris 11 上以机器可读格式获取 pkg 属性

我希望能够(从 shell 脚本中)提取 IPS 包的属性。基本上,我想要的信息与该pkg info命令列出的信息完全相同,但不幸的是,该命令列出的信息格式对于从脚本中读取来说不太友好。下面的例子:

$ pkg info archiver/gnu-tar
          Name: archiver/gnu-tar
       Summary: GNU version of the tar archiving utility
   Description: Tar is a program for packaging a set of files as a single
                archive in tar format.
      Category: Development/GNU
         State: Installed
     Publisher: solaris
       Version: 1.26
 Build Release: 5.11
        Branch: 0.175.0.0.0.2.537
Packaging Date: October 19, 2011 09:11:16 AM
          Size: 1.73 MB
          FMRI: pkg://solaris/archiver/[email protected],5.11-0.175.0.0.0.2.537:20111019T091116Z

我希望有类似的命令pkg get-property pkg.summary archiver/gnu-tar可以作为命令使用,但找不到这样的命令。我真的不想尝试解析上面的输出。其次,手册页明确指出,输出pkg info是供人类而不是机器阅读的。

答案1

大部分pkg info报道来自包中定义包属性和其他元数据的操作。对于 archiver/gnu-tar 的示例:

% pkg contents -t set -o name,value archiver/gnu-tar
NAME                          VALUE
info.classification           org.opensolaris.category.2008:Development/GNU
info.source-url               http://ftp.gnu.org/gnu/tar/tar-1.26.tar.bz2
info.upstream-url             http://www.gnu.org/software/tar/
org.opensolaris.arc-caseid    PSARC/2000/488
org.opensolaris.consolidation userland
pkg.description               Tar is a program for packaging a set of files as a single archive in tar format.
pkg.fmri                      pkg://solaris/archiver/[email protected],5.11-0.175.1.0.0.20.0:20120709T173816Z
pkg.summary                   GNU version of the tar archiving utility
variant.arch                  ['i386', 'sparc']

% pkg contents -H -t set -o value -a name=pkg.summary archiver/gnu-tar
GNU version of the tar archiving utility

答案2

以下是我通过解析 pkg 信息输出来实现的方法:

$ pkg info archiver/gnu-tar | nawk '
/^ *[A-Za-z ]*:/ {
    gsub("^ *","",$1)
    if(NR>1) printf("\n")
    name=substr($0,1,index($0,":")-1);
    value=substr($0,index($0,":")+1);
    gsub(" ","_",name);
    printf("%s=%s",name,value)
    next
}
{
  gsub("^ *","",$1)
  printf("%s",$0)
} ' | sed -e 's/= /="/' -e 's/$/"/'

输出:

Name="archiver/gnu-tar"
Summary="GNU version of the tar archiving utility"
Description="Tar is a program for packaging a set of files as a singlearchive in tar format."
Category="Development/GNU"
State="Installed"
Publisher="solaris"
Version="1.26"
Build_Release="5.11"
Branch="0.175.0.0.0.2.537"
Packaging_Date="October 19, 2011 09:11:16 AM"
Size="1.73 MB"
FMRI="pkg://solaris/archiver/[email protected],5.11-0.175.0.0.0.2.537:20111019T091116Z"

相关内容