目的
生成 size 大小,默认内容为 0xFF 的文件
shell 脚本
将如下脚本保存为 generate_blank_bin.sh
#!/bin/bash
if [ $# -lt 1 ]; then
echo -e "\033[31mError Parameter counts:$#, exit\033[0m"
exit
elif [ $# -eq 2 ]; then
tmp=`echo "obase=8;ibase=16;$2" | bc`
character=`echo "\\\\$tmp"`
else
character='\377'
fi
filename=`echo blank_$1.bin`
block_size=4096
block_counts=`expr $1 / $block_size`
remaining_size=`expr $1 % $block_size`
tr '\000' $character < /dev/zero | dd of=$filename bs=$block_size count=$block_counts
if [ $remaining_size -gt 0 ]; then
tr '\000' $character < /dev/zero | dd of=$filename bs=$remaining_size count=1 oflag=append conv=notrunc
fi
用法
- 用法 1:
./generate_blank_bin.sh size - 用法 2:
./generate_blank_bin.sh size hex
size: 自然数, 例如:1,1024,4096000
hex: 十六进制数, 例如:11,AB,FF
用法测试
1): 生成 4KB 大小的 blank bin
./generate_blank_bin.sh 4096
将会在当前目录生成一个 blank_4096.bin, 其大小为 4KB, 内容为 0xFF
2): 生成 100 字节,内容为 0xCC 的文件
./generate_blank_bin.sh 100 CC
将会在当前目录生成一个 blank_100.bin, 其大小为 100字节, 内容为 0xCC
本文介绍如何通过Shell脚本生成一个指定大小的文件,文件内容默认为0xFF。提供两种用法,可以自定义文件大小和内容,并在当前目录创建相应文件。
2913





