时间:2022-08-31 13:33
人气:
作者:admin
笔者最近在项目自测试中,有个小小的需求:需要在原始文件的后面,追加一个**固定大小**的文件,组成一个更大的文件做测试,并且为了保证测试结果的准确性和随机性,这个固定大小的文件还必须是内容随机的。经过学习和整理,发现在**linux命令行**有个非常有用的命令**dd**就可以解决此问题。
为了今后也能快速使用这个功能,我把这个功能的实现,用shell脚本文件的形式编写出来,需要生成文件的时候只需要在命令行里,按照约定的命令行参数输入对应的配置信息,就可以生成所需的文件,方便、快捷,高效工作就从此刻开始。
废话不说,直接上脚本内容:
#########################################################################
# File Name: random-file.sh
# Author : szullc
# Created Time: 2018年12月03日 星期一 11时28分23秒
#########################################################################
#!/bin/bash -e
out_file_name=$1
file_size=$2
size_unit=$3
tmp_out_file_name=$out_file_name.tmp
function check_input_param()
{
if [[ "a" == "a"$out_file_name || "a" == "a"$file_size || "a" == "a"$size_unit ]]; then
echo "Error param input !"
echo "Type in like this: $0 [out-file-name] [file-szie] [size-unit]"
echo "param list as follow:"
echo "[out-file-name]: input your output file name, Relative path and absolute path are OK."
echo "[file-size]: The file size of output file, which must be an integer."
echo "[size-unit]: Only support K/M/G. They mean xxxKB/xxxMB/xxxGB."
exit
fi
}
function check_file_size_if_integer()
{
if [ -n "$file_size" -a "$file_size" = "${file_size//[^0-9]