beanshell脚本构造生成随机大小的文件

文件下载地址:链接: https://pan.baidu.com/s/1wum8hfBeLMipdtQlqysp8A?pwd=8e7r 提取码: 8e7r

#!/bin/bash -e
# sh filerandomsize.sh /opt/xntest/1MB ftp_file_1M_10090917.txt 1 M 1000
# sh filerandomsize.sh /opt/xntest/100MB ftp_file_100M_10090917.txt 100 M 100
# sh filerandomsize.sh /opt/xntest/1GB ftp_file_1g_10090917.txt 1024 M 10
# sh filerandomsize.sh /opt/xntest/10GB ftp_file_10g_10090917.txt 10240 M 2

out_file_path=$1
out_file_name=$2
file_size=$3
size_unit=$4
file_count=$5
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]/}" ]; then
echo "file_size=$file_size"
else
echo "[file-size] error: The file size of output file, which must be an integer."
exit
fi
}

function check_file_count_if_integer()
{
if [ -n "$file_count" -a "$file_count" = "${file_count//[^0-9]/}" ]; then
echo "file_count=$file_count"
else
echo "[file-count] error: The file count of output file, which must be an integer."
exit
fi
}

function check_size_unit()
{
if [[ "K" != $size_unit && "M" != $size_unit && "G" != $size_unit ]]; then
echo "[size-unit] error: Only support K/M/G. They mean xxxKB/xxxMB/xxxGB."
exit
fi
}

function create_random_file()
{
dd if=/dev/urandom of=$tmp_out_file_name oflag=direct bs=1$size_unit count=$file_size conv=notrunc
mv $tmp_out_file_name $out_file_name
}

check_input_param
check_file_size_if_integer
check_file_count_if_integer
check_size_unit
create_random_file

if ((file_count>1));then

for ((i=1;i<=file_count;i++))
do
#out_file_name="$file_name-multiple-$i.txt"
echo "yes|cp -rf $out_file_name $out_file_path/multiple-$i-$out_file_name"
yes|cp -rf $out_file_name "$out_file_path/multiple-$i-$out_file_name"
done
echo "$file_count files created: $out_file_path"
else
echo "$out_file_path$out_file_name"
yes|cp -rf $out_file_name $out_file_path
echo "1 file created: $out_file_path"
fi

posted on 2022-10-26 16:05  seamy  阅读(44)  评论(0编辑  收藏  举报