我需要将从/dev/uradom获得的5MB数据块写入分区中的特定位置.然后,我需要判断写入是否正确执行.我以前在C++中成功地做到了这一点,但现在我想在bash脚本中实现它.
我的C++代码包括:
- 创建从/dev/uradom(RANDOM_ARRAY)填充的10MB数据数组
- 使用open()打开分区
- 使用lseek()导航到分区中所需的位置
- 使用write()将数组写入分区
- 关闭并重新打开分区,使用lseek导航回所需位置
- 使用read()读取此位置的5MB,并用此数据填充另一个数组(WRITTEN_ARRAY)
- 比较(RANDOM_ARRAY)和(WRITTEN_ARRAY)中的每个元素
我没有编写bash脚本的经验,但到目前为止,我已经做到了这一点,尽管它似乎不起作用:
random_data="$(cat /dev/urandom | head -c<5MB>)"
printf $random_data | dd of=<partition_path> bs=1 seek=<position_in_partition> count=<5MB>
file_data=$(dd if=<partition_path> bs=1 skip=<position_in_partition> count=<5MB>)
if [ "$random_data" == "$file_data" ]
then
echo "data write successful"
fi
多亏了有帮助的 comments 者,我的脚本现在看起来像这样:
# get 10MB random data
head -c<10MB> /dev/urandom > random.bin
# write 10MB random data to partition
dd if=random.bin of=<partition_location>
# copy the written data
dd if=<partition_location> count=<10MB/512 bytes> of=newdata.bin
# compare
cmp random.bin newdata.bin
此时,cmp返回的第一个字符不同.查看cmp的详细输出,并得出newdata中的所有值.bin为0.