Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to be able to extract (from within a shell script) the attributes of an IPS package. Basically I'm after exact same information as is listed with the pkg info command, but unfortunately this command list information in a format which is not very friendly to read from within a script. Example below:

$ 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/gnu-tar@1.26,5.11-0.175.0.0.0.2.537:20111019T091116Z

I was hoping for something like pkg get-property pkg.summary archiver/gnu-tar to be available as a command but can't find such command. I would really hate to try to parse the above output. Secondly the man pages clearly states that the output from pkg info is intended to be read by humans, not machines.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Most of what pkg info reports comes from the set actions in the package which define package attributes and other metadata. For your example of 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/gnu-tar@1.26,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
share|improve this answer
    
Perfect! You variation of pkg contents was indeed what I was looking for. Not very logical that you would use the 'set' action when all you want is to display a value. I would NEVER have figured this out by reading the pkg man page. –  nolan6000 Aug 20 '12 at 18:47

Here is how I would do it by parsing pkg info output:

$ 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/$/"/'

Output:

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/gnu-tar@1.26,5.11-0.175.0.0.0.2.537:20111019T091116Z"
share|improve this answer
    
Thank you so much. That is truly an amazing piece of AWK code. I wonder what would happen if the Description field would contain a colon? I'll leave the question open for some days to see if somebody can come up with a native command on how to do this. If nothing comes along I'll accept your answer. Thanks again buddy. –  nolan6000 Aug 16 '12 at 10:43
    
Nothing will happen if the description field contains a colon as only the first one is processed. By the way, I just enhanced my script to replace spaces by underscores in the field names for the script to output valid shell script assignments. –  jlliagre Aug 16 '12 at 10:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.