gpt4 book ai didi

bash 从 fcntl 锁定文件中排除循环

转载 作者:行者123 更新时间:2023-12-04 23:11:32 26 4
gpt4 key购买 nike

我有一个简单的脚本,它遍历文件夹中的文件,然后将它们从 flv 转换为 mp4。如何在 bash 中跳过文件夹中具有 的文件fcntl 锁定它们,然后在解除锁定时返回?


#!/bin/bash


for file in /video_recordings/stream/*.flv; do

today=`date '+%Y_%m_%d_%H_%M_%S'`;

cp -v "$file" /video_recordings/stream/backups/$today.flv;

[ -e "${file%.flv}".mp4 ] || ffmpeg -threads 1 -i "$file" -c:v libx264 -c:a aac -b:v 768k -b:a 96k -vf "scale=720:trunc(ow/a/2)*2" -tune fastdecode -preset ultrafast -crf 25 /video_recordings/stream/temp/$today.mp4

cp -v /video_recordings/stream/temp/$today.mp4 /video_recordings/stream/vod/$today.mp4

rm /video_recordings/stream/temp/$today.mp4

sleep 15s;

rm "$file";

done


最佳答案

我不太了解fcntl , 但根据 this answer此类锁应由 fuser 列出.如果有东西正在访问文件fuser以状态码 0 退出,否则以 1 退出。
要稍后返回锁定的文件,请使用数组,就像它是一个队列一样:

#!/bin/bash
queue=(/video_recordings/stream/*.flv)
while (( ${#queue[@]} > 0 )); do
file="${queue[0]}"
queue=("${queue[@]:1}")
if fuser -s "$file"; then
echo "$file in use. Try later."
queue+=("$file")
continue
fi
# insert commands for conversion of `$file` here
done

关于bash 从 fcntl 锁定文件中排除循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66786312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com