Below is a section from the dmidecode output. There is a part of this section that I want to break down. I want to get the 4 bytes after pattern 01 85 30. And I want to change the byte order. Then I will compare with the hex strings I have.
.......
Handle 0x0027, DMI type 219, 106 bytes
OEM-specific Type
Header and Data:
DB 6A 27 00 01 04 01 45 02 00 90 06 *01 85 30* 20
00 00 00 00 40 00 00 03 1F 24 02 C9 02 60 44 02
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF 03 00 00 00 80 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 04 FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF
.......
I prepared a bash script and it works, is there a better way?
#!/bin/bash
id_array=("00000020", "00000040")
unique_id=$(dmidecode -t 219 | grep "01 85 30" -A 1 | xargs | cut -c 46-56 | tr -d '[:space:]' | grep -o .. | tac | paste -sd '' -)
if [[ " ${id_array[*]} " == *"$unique_id"* ]];
then
echo "1"
else
echo "0"
fi
1 Answer 1
Your script should work, but there is room for improvement. Instead of using grep
and cut
, you can use awk
to extract the bytes after the pattern 01 85 30 and reverse their order. Here's an example:
unique_id=$(dmidecode -t 219 | awk '/01 85 30/ {getline; gsub(/ /,""); print substr(0,9,8ドル)}' | tac -rs ..)
This awk command searches for the pattern 01 85 30, reads the next line, removes all spaces, and prints the 4 bytes starting from the 9th position (which is after the 8-byte timestamp). The result is then piped to tac -rs ..
to reverse the byte order.
In addition, you can simplify the array search by using parameter expansion instead of grep:
if [[ ${id_array[*]} =~ $unique_id ]]; then
echo "1"
else
echo "0"
fi
This tests whether the expanded array ${id_array[*]}
contains the string $unique_id
. The =~
operator is used for pattern matching.
-
\$\begingroup\$ Thanks your finger but not worked, after the pattern 01 85 30. There is a 20. But you eliminated it. \$\endgroup\$embeddedstack– embeddedstack2023年02月18日 10:52:59 +00:00Commented Feb 18, 2023 at 10:52
-
\$\begingroup\$ @embedded4ever maybe add ` | tail -n1 | tac -rs ..` instead of just ` | tac -rs ..` ? \$\endgroup\$Simon– Simon2023年02月18日 11:01:03 +00:00Commented Feb 18, 2023 at 11:01
-
\$\begingroup\$ Nothing changed, because you have already eliminated once using getline. \$\endgroup\$embeddedstack– embeddedstack2023年02月18日 11:02:40 +00:00Commented Feb 18, 2023 at 11:02
-
\$\begingroup\$ @embedded4ever oh, then
print substr(0,11,8ドル)
? sorry for guessing, I can't check that out for myself right now \$\endgroup\$Simon– Simon2023年02月18日 11:05:33 +00:00Commented Feb 18, 2023 at 11:05 -
\$\begingroup\$ i see, but not worked again :) \$\endgroup\$embeddedstack– embeddedstack2023年02月18日 11:09:00 +00:00Commented Feb 18, 2023 at 11:09