【问题标题】:Checking for files in a directory with bash script ? what is wrong with this code?使用 bash 脚本检查目录中的文件?这段代码有什么问题?
【发布时间】:2017-04-18 02:36:25
【问题描述】:

我编写了这段代码来检查目录中的视频文件并使用 ffmpeg 转换它们。 在运行此脚本时,我将其输出为“[ERROR] File Not Found”,我将它放在 else 块中。我没有获取文件的脚本有什么问题。

#!/bin/bash
# set PATH to check existance of video file in this directory
checkfiles='/home/webuser/public_html/shareportal/convertedUp_videos/'
#format of output video file
webm='webm'
for f in checkfiles
do
fullfilename="$f"
filenamewithpath=$(basename "$fullfilename")
filewithoutext="${filename%.*}"
fileextention="${filename##*.}"
changeextension=mv --"$f" "${f%.$fileextention}.$webm"
outputfilename="/home/webuser/public_html/shareportal/converted_videos/$changeextension"
echo "File FUll NAME : $fullfilename"
echo "File name with full path : $filenamewithpath"
echo "File name without extention : $filewithoutext"
echo "File extention : $fileextention"
echo '1 File Converted'
if (ffmpeg -i "$f" "$outputfilename")
then
confullfilename="$outputfilename"
confilenamewithpath=$(basename "$confullfilename")
confilewithoutext="${filename%.*}"
confileextention="${filename##*.}"
echo "File FUll NAME : $confullfilename"
echo "File name with full path : $confilenamewithpath"
echo "File name without extention : $confilewithoutext"
echo "File extention : $confileextention"
echo '1 File Converted'
else
echo "Could Not Convert File"
fi
#get Image of video file on provided time stamp
image_path='/home/webuser/public_html/shareportal/video_images/$filewithoutext.png'
if (ffmpeg -ss 00:00:03 -i "$f" -vframes:v 1 "$image_path")
then
echo "Image Extracted"
else
echo "Could not Extract Image"
fi
done
rm -r f

【问题讨论】:

  • 那么普通文件和目录中的文件有什么区别呢?
  • 它不检查目录中的文件。 -f 检查该确切路径是否为文件。目录不是常规文件。您需要在 for 循环中进行检查,该循环遍历文件列表并检查每个文件。
  • 您建议的副本:文件的扩展名是 .txt,我们可以静态设置它,但这里的扩展名因视频格式而异,我需要先检查扩展名吗?
  • 不,这是不正确的。 bash for 循环遍历给定项目的列表。它不知道目录或文件。您不能只为for 循环提供一个目录并期望它循环文件。这不是它的工作方式。如链接所示,您需要执行for f in ${checkfiles}*,其中${checkfiles}* 将展开以列出该目录中的所有文件(就像ls *

标签: linux bash ubuntu ffmpeg


【解决方案1】:

How to iterate over files in directory with bash?

bash for 循环遍历给定项目的列表。它不知道目录或文件。您不能只为for 循环提供目录并期望它循环文件。这不是它的工作方式。如链接所示,您需要执行for f in ${checkfiles}<em></em>,其中${checkfiles} 将展开以列出该目录中的所有文件(就像ls *) – 凯勒姆

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 2018-09-09
    相关资源
    最近更新 更多