gpt4 book ai didi

读取循环时的 Shell 脚本仅执行一次

转载 作者:行者123 更新时间:2023-12-04 08:29:22 25 4
gpt4 key购买 nike

我正在编写一个 shell 脚本,通过 Handbrake 从我的相机中批量处理 .mov 文件以节省高清空间。该脚本使用“find”搜索目录,然后对找到的每个 .mov 文件运行 Handbrake,使用“touch”将结果文件的创建日期与源文件的日期匹配。

我最初是用 做的为 环形:

for i in $(find "$*" -iname '*.mov') ; do
~/Unix/HandbrakeCLI --input "$i" --output "$i".mp4 --preset="Normal"
touch -r "$i" "$i".mp4
done

这有效,但如果输入文件的文件名中有空格,则失败。所以我尝试了一个 改为循环:
find "$*" -iname '*.mov' | while read i ; do
~/Unix/HandbrakeCLI --input "$i" --output "$i".mp4 --preset="Normal"
touch -r "$i" "$i".mp4
done

这个循环的问题在于它适用于目录中的第一个文件,然后退出循环。请注意,如果我在 while 循环的主体中替换了“echo $i”,它将打印目录中的所有 .mov 文件,因此循环结构正确。

我相信我在 this stackoverflow thread 上的问题有部分答案.但该解决方案特定于 ssh,并不能解决一般问题。似乎与子进程使用的标准输入有关,但我并不完全理解这是如何工作的。

有什么建议吗?

我在 OSX 10.6

最佳答案

取自 this answer :我现在在 HandbrakeCLI 中没有回显任何内容,以确保它没有使用与我的脚本相同的标准输入:

find . -name "*.mkv" | while read FILE
do
echo "" | handbrake-touch "$FILE"

if [ $? != 0 ]
then
echo "$FILE had problems" >> errors.log
fi
done

...它按预期/预期工作。

关于读取循环时的 Shell 脚本仅执行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5549405/

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