How to identify the name (and only the name) of a PCI device?

#!/bin/bash

PCI_IDS=$(find /usr/share -maxdepth 2 -name "pci.ids" -print -quit) #Some distros don't store it in the same place.

for CARD in /sys/class/drm/card[0-99]/device;
do
	source $CARD/uevent
	IFS=: PCI_ID=(${PCI_ID,,}) PCI_SUBSYS_ID=(${PCI_SUBSYS_ID,,})

	VENDOR_DEVICELIST=$(awk "/^${PCI_ID[0]}/{a=1;next}/^....  /{a=0}a" $PCI_IDS)

	NAME=$(sed -n "0,/\s*${PCI_SUBSYS_ID[0]} ${PCI_SUBSYS_ID[1]}  /s///p" <<< $VENDOR_DEVICELIST)
	[ -z $NAME ] && NAME=$(sed -n "0,/\s*${PCI_ID[1]}  /s///p" <<< $VENDOR_DEVICELIST)
	NAME=$(awk -F'[][]' '{if ($2) {print $2} else {print $0}}' <<< $NAME)

	echo $NAME
done

It gets a little messy there at the end, but this should work now for both your pc and mine. It’ll try to find a refernece to the subsystem_id under the vendor’s device list, and if it cannot be found it defaults to the device id instead.

So it should always give the most specific result available now.

I would have liked to set $NAME in one line instead of 3. But if it works, it works.

Have you considered to use the info provided by

lspci -vmmnn

It might be easier to handle, but that depends on what your goals are.

Doesn’t suit me better no, but that is a pretty cool command.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.