【备忘】bash写随机数以及find ./抓到的路径放入array

据说这个写法是真随机,咱也不懂。反正假神也能当真神用啦。

#!/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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取啥都被占用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值