#!/bin/bash start_addr=0x05877000 end_addr=0x3ffff000 data_write=0x55AA data_read=$(printf "%08x" $data_write) count=0 match_count=0 #while [ $(($start_addr+0x32000)) -lt $(($end_addr)) ]; 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 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)) done 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%)"请给这段代码添加以1%为单位输出写入进度的功能。
时间: 2024-02-06 09:04:01 浏览: 127
解决-BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: 权限不够问题
可以在写入循环中加入进度条的输出。以下是修改后的代码:
```
#!/bin/bash
start_addr=0x05877000
end_addr=0x3ffff000
data_write=0x55AA
data_read=$(printf "%08x" $data_write)
count=0
match_count=0
for ((addr=$start_addr; addr<$end_addr; addr=addr+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
echo "Writing: 100%"
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))
percent=$(echo "scale=2; $count * 100 / (($end_addr - $start_addr) / 4)" | bc)
echo -ne "Reading: $percent%\r"
done
echo "Reading: 100%"
if [ $match_count -eq $count ]; then
echo "PASS"
else
echo "FAIL"
fi
```
代码中加入了一个写入进度条和一个读取进度条。在写入循环中,我们使用当前地址与开始地址的差值除以总地址差值的百分比来计算进度。在读取循环中,我们使用已读取的数据块数除以总块数的百分比来计算进度。注意,这两个进度条都使用 echo -ne 命令输出,并使用 \r 字符将光标移动到行首,以实现进度条的更新效果。
阅读全文