start_addr=0x30000000 end_addr=0x30000003 data_write=0x55AA55AA count=0 match_count=0 # 写入数据 for ((addr=$start_addr; addr<$end_addr+1; addr=addr+1)); do echo -ne "\x55\xAA\x55\xAA" | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/null data=$(echo -ne "\x55\xAA\x55\xAA" | hexdump -e '/4 "%08x\n"') echo "data: $data" data_read=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') echo "data_read: $data_read" if [ "$data_read" == "$data" ]; then match_count=$((match_count+1)) echo "match_count: $match_count" fi count=$((count+1)) echo "count: $count" done # 输出结果 # 判断结果 if [ $match_count -eq $count ]; then echo "PASS" else echo "FAIL" fi 为什么这段代码data和data_read输出的都是空白?
时间: 2024-03-01 14:51:34 浏览: 121
sample_priv_addr.rar_无线网卡_设置 网卡
这段代码中,数据写入和读取操作都使用了`dd`命令和`hexdump`命令。但是,`dd`命令和`hexdump`命令都是用于二进制数据的传输和显示,而不是文本数据。因此,如果想要在终端中查看二进制数据,需要使用`xxd`命令或者`od`命令。所以,将`hexdump`命令替换成`xxd`命令或者`od`命令即可输出正确的数据。
修改后的代码如下:
```
start_addr=0x30000000 end_addr=0x30000003 data_write=0x55AA55AA count=0 match_count=0
# 写入数据
for ((addr=$start_addr; addr<$end_addr+1; addr=addr+1)); do
echo -ne "\x55\xAA\x55\xAA" | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/null
data=$(echo -ne "\x55\xAA\x55\xAA" | xxd -p)
echo "data: $data"
data_read=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | xxd -p)
echo "data_read: $data_read"
if [ "$data_read" == "$data" ]; then
match_count=$((match_count+1))
echo "match_count: $match_count"
fi
count=$((count+1))
echo "count: $count"
done
# 判断结果
if [ $match_count -eq $count ]; then
echo "PASS"
else
echo "FAIL"
fi
```
阅读全文