You are not logged in.

#1 2013-02-23 21:42:31

the_metalgamer
Member
From: .lu
Registered: 2011-08-19
Posts: 4
Website

[SOLVED] Parsing output of pacman -Qm to an bash array

Hey guys,

I'm currently writing a small bash script to check if there are any updates for my packages installed from the AUR. For this I need to get an array from the output of

pacman -Qm

. I've tried this

packages=(`pacman -Qm`)

, but this isn't that what I've intended for.

I want to have an array like this

packages=("packagename version", "packagename version" ...)

Does anybody know the way, how to solve this problem?

Best regards,
the_metalgamer

Last edited by the_metalgamer (2013-02-23 22:24:11)

Offline

#2 2013-02-23 22:11:29

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,114
Website

Re: [SOLVED] Parsing output of pacman -Qm to an bash array

┌─[Centurion ~]
└─╼ packages=(); IFS=\n; while read -r; do packages+=("$REPLY"); done < <(pacman -Qm)
┌─[Centurion ~]
└─╼ echo "${#packages}"
22
┌─[Centurion ~]
└─╼ echo "${packages[3]}"
aurvote 1.5-1

See Greg's Wiki for details...


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Online

#3 2013-02-23 22:23:52

the_metalgamer
Member
From: .lu
Registered: 2011-08-19
Posts: 4
Website

Re: [SOLVED] Parsing output of pacman -Qm to an bash array

Thank you very much indeed.

Best regards,
the_metalgamer

Offline

#4 2013-02-24 05:52:29

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 216

Re: [SOLVED] Parsing output of pacman -Qm to an bash array

IFS=\n

I don't believe that does what you think it does. It sets IFS to be the string '\n' and NOT a newline.

Try this

IFS=$'\n' packages=($(pacman -Qm))

Offline

#5 2013-02-24 07:02:33

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,114
Website

Re: [SOLVED] Parsing output of pacman -Qm to an bash array

rockin turtle wrote:

IFS=\n

I don't believe that does what you think it does. It sets IFS to be the string '\n' and NOT a newline.


You are right, I should have quoted the newline. Oddly, the loop still reads the entries in using the newline as the separator; as the output shows...


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Online

#6 2013-03-02 14:00:36

sh33p
Member
Registered: 2013-03-02
Posts: 2

Re: [SOLVED] Parsing output of pacman -Qm to an bash array

Just to clarify this:

With the solution in post #2 the IFS variable will contain just an "n". So any package name containing an "n" will be cut into pieces.

$ echo ${packages[1]}
e emyli es3 1.2-1

The correct splitting is not caused by the IFS variable but by the read command which ALWAYS stops reading at a newline no matter what the IFS is set to. So there is no need to set the IFS variable to anything different and just use

packages=(); while read -r; do packages+=("$REPLY"); done < <(pacman -Qm)

Offline

Board footer

Powered by FluxBB