1

I'm trying to make a oneliner to be able to list all locked (forbidden to upgrade) packages in FreeBSD.

Normally, you issue this command:

pkg info -ak

It outputs list of all packages in system including locked and unlocked which isn't working for me as I only want locked ones. If I grep 'yes' it does exactly what I want:

pkg info -ak | grep yes

It outputs following:

nginx-devel-1.5.10             yes
php5-extensions-1.7            yes
php55-5.5.8                    yes

Now, what I want is to be able to edit that output a little bit by changing string 'yes' for following (adding package name w/ version omitted):

nginx-devel-1.5.10             locked, run: sudo pkg unlock nginx-devel
php5-extensions-1.7            locked, run: sudo pkg unlock php5-extensions
php55-5.5.8                    locked, run: sudo pkg unlock php55

I tried following unsuccessfully:

pkg info -ak | grep yes | sed s/yes/"locked - run: sudo pkg unlock $(pkg info -ak | grep yes | cut -d - f1-2)"/

While what I've tried isn't working I posted it just for you to get the idea what I'm trying to achieve.

2 Answers 2

2

The pkg command has been updated since this answer and pkg lock -l will produce the list of locked packages:

$ pkg lock -l
Currently locked packages:
compat11x-amd64-11.2.1102000.20181014
phantomjs-2.1.1_15

and

man pkg-lock

will give the details in any up-to-date version of FreeBSD.

For modern versions of FreeBSD, pkg lock and awk can provide your solution:

$ pkg lock -l |
  tail +2 |
  awk '{print $1}' |
  awk 'BEGIN{FS=OFS="-"} {\
    printf "%-33s locked, run: sudo pkg unlock ", $0; NF--; \
    printf "%s\n", $0 \
  }'

Output:

compat11x-amd64-11.2.1102000.20181014 locked, run: sudo pkg unlock compat11x-amd64
phantomjs-2.1.1_15                locked, run: sudo pkg unlock phantomjs

Note also, however, that removing the package version number is not necessary. pkg unlock will accept the full package name, including the version number. Hence, this simpler syntax is equally usable in terms of the unlocking actions recommended:

$ pkg lock -l |
  tail +2 |
  awk '{printf "%-33s locked, run: sudo pkg unlock %s\n", $1, $1}'

Output:

compat11x-amd64-11.2.1102000.20181014 locked, run: sudo pkg unlock compat11x-amd64-11.2.1102000.20181014
phantomjs-2.1.1_15                locked, run: sudo pkg unlock phantomjs-2.1.1_15
2
  • How can the OP use this to get the output they want? … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete. Commented Jul 9, 2022 at 3:18
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jul 14, 2022 at 14:31
1
pkg info -ak | grep -w yes | while read line; do packnum=`echo $line | awk '{print $1}'`; pack=`echo $packnum | awk 'BEGIN{FS=OFS="-"}{$NF=""; NF--; print}'` ; printf "%-33s%s\n" "$packnum" "locked, run: sudo pkg unlock$pack" ;done

I added -w option to your grep to avoid confusion with packages containing "yes" in their name string.

2
  • Hey @Slyx this is the output i get: nginx-devel-1.5.10 locked, run: sudo pkg unlock devel 1.5.10 So first package name field is missing and package version isn't omitted. Any chance of sorting those? Thanks man, appreciate it!
    – dzhi
    Commented Feb 5, 2014 at 19:31
  • Indeed ! I fixed the mistake, please see my edit !
    – Slyx
    Commented Feb 5, 2014 at 22:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .