start_addr=0x30000000 end_addr=0x30000003 data_write=0x55AA55AA count=0 match_count=0 for ((addr=$start_addr; addr<$end_addr; addr=addr+4)); do echo -ne "\x55\xAA\x55\xAA" | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/null data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') done for ((addr=$start_addr; addr<$end_addr; addr+=4)); do data_read=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') if [ "$data_read" == "$data" ]; then match_count=$((match_count+1)) fi count=$((count+1)) done echo $data echo $data_read echo $match_count echo $count if [ $match_count -eq $count ]; then echo "PASS" else echo "FAIL" fi这段代码为什么match_count和count输出的是1,我的设想是从起始地址到终止地址一共四个地址,不应该是比较4次吗?请注意我想比较的是data_read和data,请在满足我想比较的数据的前提下完成count数值等于从起始地址到终止地址这一要求。
时间: 2024-03-01 22:50:28 浏览: 97
在您的代码中,循环的终止条件是`$end_addr`,而不是`$end_addr + 1`。因此,实际上只有一个地址被写入数据,只有一次比较,所以`match_count`和`count`都是1。
为了满足您的要求,需要将循环的终止条件修改为`$end_addr + 1`,将写入数据的循环和比较数据的循环分开。具体来说,修改后的代码如下:
```bash
start_addr=0x30000000
end_addr=0x30000003
data_write=0x55AA55AA
count=0
match_count=0
# 写入数据
for ((addr=$start_addr; addr<$end_addr+1; addr=addr+4)); do
echo -ne "\x55\xAA\x55\xAA" | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/null
done
# 比较数据
for ((addr=$start_addr; addr<$end_addr+1; addr=addr+4)); do
data=$(echo -ne "\x55\xAA\x55\xAA" | hexdump -e '/4 "%08x\n"')
data_read=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"')
if [ "$data_read" == "$data" ]; then
match_count=$((match_count+1))
fi
count=$((count+1))
done
# 输出结果
echo "data: $data"
echo "data_read: $data_read"
echo "match_count: $match_count"
echo "count: $count"
# 判断结果
if [ $match_count -eq $count ]; then
echo "PASS"
else
echo "FAIL"
fi
```
以上代码中,我们将写入数据的循环和比较数据的循环分开,并且将循环的终止条件修改为`$end_addr + 1`,从而确保从起始地址到终止地址一共四个地址被写入数据,比较四次。
阅读全文