据说这个写法是真随机,咱也不懂。反正假神也能当真神用啦。
#!/bin/bash
#
#
target_directory="/home/wwwroot/blog/wp-content/uploads/"
target_file_name="/home/wwwroot/blog/wp-content/uploads/2024/05/crawling_crabs.mp4"
mv_directory="/home/wwwroot/blog/wp-content/uploads/used_mp4/"
function random()
{
min=$1;
max=$2-$1;
num=$(date +%s+%N);
((retnum=num%max+min));
#进行求余数运算即可
echo $retnum;
#这里通过echo 打印出来值,然后获得函数的,stdout就可以获得值
#还有一种返回,定义全价变量,然后函数改下内容,外面读取
}
IFS=$'\n'
mp4_files=($(find "/home/wwwroot/blog/wp-content/uploads" -name "*.mp4"))
unset IFS
array_len=${#mp4_files[@]}
rand=$(random 0 $array_len)
#echo $rand
rand_file=${mp4_files["$rand"]}
#echo $rand_file
if [ "$rand_file" == "$target_file_name" ]; then
exit 0
fi
new_mp4="$mv_directory""$(date +"%Y%m%d_%H%M%S" -r $target_file_name)"'.mp4'
mv "$target_file_name" "$new_mp4"
mv "$rand_file" "$target_file_name"
chgrp www -R $target_directory
chown www -R $target_directory
chmod 755 -R $target_directory
#echo ${#mp4_files[@]} #length of an array
#echo ${arr_mp4[2]} #retrieve specific array item.