for ((addr=$start_addr; addr<$end_addr; addr=addr+4)); do echo -ne "\x55\xAA\x55\AA" | dd of=/dev/mem bs=2 count=2 seek=$(($addr/8)) conv=notrunc 2>/dev/nul data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') #percent=$(echo "scale=2; ($addr - $start_addr) * 100 / ($end_addr - $start_addr)" | bc) # echo -ne "Writing: $percent%\r" done # echo $data for ((addr=$start_addr; addr<$end_addr; addr+=4)); do data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') if [ "$data_write" == "$data_read" ]; then match_count=$((match_count+1)) fi count=$((count+1)) # percent=$(echo "scale=2; $count * 100 / (($end_addr - $start_addr) / 4)" | bc) # echo -ne "Reading: $percent%\r" done #echo $data echo $data_read echo $data_write 如何把这段程序中的data_read改为和data_write一样拥有0x前缀的格式?
时间: 2024-02-28 07:52:26 浏览: 110
要将 `data_read` 的输出格式改为带有 `0x` 前缀的十六进制数,可以使用 `printf` 函数中的格式化字符串 `%#x`,其中 `#` 表示要输出 `0x` 前缀。因此,可以将第一个 `echo` 语句中的 `data_read` 输出改为:
```
printf "data_read = %#x\n" $data_read
```
这样输出 `data_read` 的值时,就会在前面加上 `0x` 前缀,表示这是一个十六进制数,与 `data_write` 相同的格式一致。
相关问题
for ((addr=$start_addr; addr<$end_addr; addr=addr+4)); #for ((i=start_addr;i<=end_addr;i+=4)); do echo -n "\x55\xAA" | dd of=/dev/mem bs=4 count=1 seek=$(($addr)) conv=notrunc 2>/dev/null # percent=$(echo "scale=2; ($addr - $start_addr) * 100 / ($end_addr - $start_addr)" | bc) # echo -ne "Writing: $percent%\r" done for ((addr=$start_addr; addr<$end_addr; addr+=4)); #for ((i=start_addr;i<=end_addr;i+=4)); do data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') if [ "$data" == "$data_read" ]; then match_count=$((match_count+1)) fi count=$((count+1)) # percent=$(echo "scale=2; $count * 100 / (($end_addr - $start_addr) / 4)" | bc) # echo -ne "Reading: $percent%\r" done #echo "Reading: 100%" echo $data if [ $match_count -eq $count ]; then echo "PASS" else echo "FAIL" fi #percent=$(echo "scale=2; $match_count * 100 / $count" | bc) #echo "Matched data: $match_count/$count ($percent%)" 这段代码是不是没给data赋值?给如何优化?
是的,代码中并没有给`data`变量赋值。为了输出`data`变量的值,需要在读取数据时将读取到的数据赋值给`data`变量,例如:
```
for ((addr=$start_addr; addr<$end_addr; addr+=4)); do
data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"')
echo $data
done
```
这样,每次读取数据时,会将读取到的数据赋值给`data`变量,并输出`data`变量的值。
另外,为了提高程序的效率,可以将读取数据和比较数据的循环合并为一个循环,例如:
```
for ((addr=$start_addr; addr<$end_addr; addr+=4)); do
data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"')
if [ "$data" == "$data_read" ]; then
match_count=$((match_count+1))
fi
count=$((count+1))
echo -ne "Progress: $((count*100/((end_addr-start_addr)/4)))%\r"
done
if [ $match_count -eq $count ]; then
echo "PASS"
else
echo "FAIL"
fi
echo "Matched data: $match_count/$count ($(echo "scale=2; $match_count*100/$count" | bc)%)"
```
这样,每次读取数据时,同时也会比较数据,并输出当前进度,最终输出匹配数据的数量和比例。
start_addr=0x05877000 #end_addr=0x3ffff000 end_addr=0x05877002 data_write=0x55AA55AA data_read=$(printf "%08x" $data_write) count=0 match_count=0 for ((addr=$start_addr; addr<$end_addr; addr=addr+4)); do echo $data_write | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/nul data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') #percent=$(echo "scale=2; ($addr - $start_addr) * 100 / ($end_addr - $start_addr)" | bc) # echo -ne "Writing: $percent%\r" done # echo $data for ((addr=$start_addr; addr<$end_addr; addr+=4)); do data=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') if [ "$data_write" == "$data_read" ]; then match_count=$((match_count+1)) fi count=$((count+1)) # percent=$(echo "scale=2; $count * 100 / (($end_addr - $start_addr) / 4)" | bc) # echo -ne "Reading: $percent%\r" done #echo $data echo $data_read echo $data_write 如果data_read=55aa55aa,data_write=0x55AA55AA,那么该如何修改程序才能让程序输出PASS
要让程序输出PASS,需要将match_count与count进行比较。如果它们相等,说明所有写入的数据都被正确地读回,程序就可以输出PASS。因此,可以在第一个循环中,将写入数据的值改为data_read,即:
```
echo $data_read | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/nul
```
然后,在第二个循环中,添加一个判断语句,如果match_count等于count,则输出PASS,如下所示:
```
if [ $match_count -eq $count ]; then
echo "PASS"
fi
```
完整代码如下:
```
start_addr=0x05877000
end_addr=0x3ffff000
data_write=0x55AA55AA
data_read=$(printf "%08x" $data_write)
count=0
match_count=0
for ((addr=$start_addr; addr<$end_addr; addr=addr+4)); do
echo $data_read | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/nul
done
for ((addr=$start_addr; addr<$end_addr; addr+=4)); do
data=$(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
if [ $match_count -eq $count ]; then
echo "PASS"
fi
```
阅读全文