我正在尝试使用bash和dmidecode编写脚本来获取板载设备信息。我可以使用数字来提取想要的数据,但不能使用bash变量。
我尝试在命令awk '{print $x}'
中使用数字和变量,其中x
是数字或变量。
dmidecode -t10 | grep "Type" | sed "s/.*://" | tr -d '\n' | awk '{print $counter}'
如果在$后面使用数字,则会得到确切的机载设备类型,如果在$后面使用变量,则会得到机器上所有机载设备类型的列表。
echo " On Board Devices " >> SystemInfo.txt
echo " **************************************" >> SystemInfo.txt
echo >> SystemInfo.txt;
declare -i counter=1
echo >> SystemInfo.txt;
echo -n "Counter: " >> SystemInfo.txt;
echo $counter >> SystemInfo.txt
echo >> SystemInfo.txt;
while [ $counter -lt 3 ]; do
echo -n "Type: " >> SystemInfo.txt
echo `dmidecode -t10 | grep "Type" | sed "s/.*://" | tr -d '\n' | awk '{print $counter}' ` >> SystemInfo.txt;
echo -n "Status: " >> SystemInfo.txt
echo `dmidecode -t10 | grep "Status" | sed 's/.*://' | tr -d '\n' | awk '{print $counter}' ` >> SystemInfo.txt;
echo -n "Description: " >> SystemInfo.txt
echo `dmidecode -t10 | grep "Description" | sed 's/.*://' | tr -d '\n' | awk '{print $counter}' ` >> SystemInfo.txt;
echo >> SystemInfo.txt;
counter=$[ $counter+1 ]
echo $counter >> SystemInfo.txt
echo >> SystemInfo.txt;
done
我希望每种类型,状态和描述分别输出,如下所示。
Type: Video
Status: Enabled
Description: "Intel HD Graphics"
Type: Ethernet
Status: Enabled
Description: NETWORK_NAME_STRING
相反,我得到以下结果:
Type: Video Ethernet
Status: Enabled Enabled
Description: "Intel HD Graphics" NETWORK_NAME_STRING
Type: Video Ethernet
Status: Enabled Enabled
Description: "Intel HD Graphics" NETWORK_NAME_STRING