Linux下获取Pci-E设备列表

在linux系统下获pcie设备列表的shell脚本

#!/bin/bash
#
#通过dmidecode获取有关“Designation:、Type:、Current Usage:、Length:、Bus Address:的信息,并输出到/tmp/dmidecode_slot.log文件内
dmidecode -t slot | grep -E "Designation:|Type:|Current Usage:|Length:|Bus Address:" | sed 's/^[ \t]*//g' | sed '/^$/d' | grep -v "\-\-" \
 > /tmp/dmidecode_slot.log
 #在/tmp/dmidecode_slot.log文件内删除x8 x16 x2 x4每行第一份匹配的值
sed -i 's/x8 //;s/x16 //;s/x2 //;s/x4 //' /tmp/dmidecode_slot.log
sed -i 's/Express //' /tmp/dmidecode_slot.log
#获取bus address值,并赋值给busid变量
busid=`cat /tmp/dmidecode_slot.log | grep "Bus Address:" | awk -F ":" '{print $3":"$4}'|awk -F "." '{print $1}'| sort | uniq`
#通过busid循环出对应的pcie插卡设备名称,将空槽位设置未N/A,并将其值输出到 /tmp/dmidecode_slot.log文件里的bus addres 的下一行
for i in $busid
 do 
# pci_devices=$(lspci | grep "$i" |awk -F ":" '{print $NF}'| sort |uniq -c | awk 'NF{NF--}1'| awk 'NF{NF--}1')
 pci_devices=$(lspci | grep "$i" |awk -F ":" '{print $NF}'| sort |uniq -c|sed s'/^ *//g')

 if [ -n "$pci_devices" ]
 then 
 :
 else 
 pci_devices="N/A" 
 fi
 sed -i '/'"$i"'/a'"pcie_devices: $pci_devices"'' /tmp/dmidecode_slot.log
done
#
#
#去除标题,保留内容,并重新输出到dmidecode_slot1,log
cat /tmp/dmidecode_slot.log |awk -F ": " '{print $2}' > /tmp/dmidecode_slot1.log
#
#去掉busid的0000:
cat /tmp/dmidecode_slot1.log | grep 0000:| sed -i 's/\.0//;s/0000://' /tmp/dmidecode_slot1.log
#添加格式化输出分割线
sed -i 's/^/| &/g' /tmp/dmidecode_slot1.log
#
#将文件中的空格换成下划线(_),方便printf对其格式化输出
sed -i 's/ /_/g' /tmp/dmidecode_slot1.log
#将dmidecode_slot1.log内容转换为一行,并赋值为pci_list
pci_list=$(echo `cat /tmp/dmidecode_slot1.log`)
#清屏
clear
#格式化输出
echo -e "\033[033m
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
+                                      ____  __________           __    _      __                                                           +  
+                                     / __ \/ ____/  _/__        / /   (_)____/ /_                                                          +  
+                                    / /_/ / /    / // _ \______/ /   / / ___/ __/                                                          +  
+                                   / ____/ /____/ //  __/_____/ /___/ (__  ) /_                                                            +  
+                                  /_/    \____/___/\___/     /_____/_/____/\__/   v1.0-2018.10.17                                          +  
+                                                                                                                                           +\033[0m"
#
echo -e "\033[33m+------------+------------+-----------+--------+----------+---------------------------------------------------------------------------------+\033[0m" > /tmp/pcie_list.log
echo -e "\033[33m| SOLT       | TPYE       | USAGE     | LENGTH | BUS ADD  | DEVIECS                                                                         |\033[0m" >> /tmp/pcie_list.log
echo -e "\033[33m+------------+------------+-----------+--------+----------+---------------------------------------------------------------------------------+\033[0m" >> /tmp/pcie_list.log
printf "%-12s %-12s %-11s %-8s %-10s %-1s\n" $pci_list >> /tmp/pcie_list.log
echo -e "\033[33m+------------+------------+-----------+--------+----------+---------------------------------------------------------------------------------+\033[0m" >> /tmp/pcie_list.log
#将文件中的下划线(_)换成空格,还原输出
sed -i 's/_/ /g' /tmp/pcie_list.log
#将结果输出到屏幕上
cat /tmp/pcie_list.log
#删除临时文件
#rm -rf /tmp/dmidecode_slot1.log /tmp/dmidecode_slot.log

你可能感兴趣的:(Linux下获取Pci-E设备列表)