bash - 将文件数与整数 nto 进行比较 - bash 脚本
问题描述
我正在编写一个 bash 脚本来使用 FFMpeg 处理文件,具体取决于文件的数量。
我正在编写一个 if / elif 语句来检查有多少文件与字符串匹配,但首先要删除字符串末尾的一部分。比较似乎不起作用,我试过 == 和 -eq。不确定更好的做法
#!/bin/bash
#Generate Quads from 4 video files, Trio from 3 and duo from 2
#InputVariables
inputpath=$( pwd | awk -F/ '{ print $0"/" }' )
outputpath=$( pwd | awk -F/ '{ print $0"/" }' ) #TEMP TEST OUTPUT
#outputpath=$( pwd | awk -F/ '{ print "/"$2"/projects/"$5"/Library/Ref/ProcessedWitcam/"$6"/" }' )
#Command Start
if [ "$( echo $inputpath | grep -F witcam )" ]; then
#Create only 1 instance of this script to avoid clashes
if [ ! "$(ls /var/run/ | fgrep -i quadGen.pid)" ]; then
yes no | nice -n 15 touch /var/run/wrangling/quadGen.pid
echo "PID created"
for videofile in $(find *_1_BTC.mp4 -type f); do
if [ "$(ls $outputpath | fgrep -i ${videofile%_1_BTC.mp4}.mp4)" ]; then
echo "Converted $videofile Already"
else
echo find "${videofile%_1_BTC.mp4}"* | wc -l
if [ "find ${videofile%_1_BTC.mp4}* | wc -l" == 5 ]; then
echo "QUAD GEN"
timecode=$( ffmpeg -i "$videofile" 2>&1 | awk '$1 ~ /^timecode/ {print $NF}' | uniq )
ffmpeg -i ${videofile%_1_BTC.mp4}_2.mxf -i $videofile -i ${videofile%_1_BTC.mp4}_3.mxf -i ${videofile%_1_BTC.mp4}_4.mxf -filter_complex "[0:v][1:v]hstack[top]; [2:v][3:v]hstack[bottom]; [top][bottom]vstack,format=yuv420p[v]" -map "[v]" -ac 2 -flags global_header -timecode $timecode -c:v libx264 $outputpath/${videofile%_1_BTC.mp4}.mp4
elif [ "find ${videofile%_1_BTC.mp4}* | wc -l" == 4 ]; then
echo "TRIO GEN"
timecode=$( ffmpeg -i "$videofile" 2>&1 | awk '$1 ~ /^timecode/ {print $NF}' | uniq )
ffmpeg -i ${videofile%_1_BTC.mp4}_2.mxf -i $videofile -i ${videofile%_1_BTC.mp4}_3.mxf -filter_complex "[0:v][1:v][2:v]hstack=inputs=3[v]" -map "[v]" -ac 2 -flags global_header -timecode $timecode -c:v libx264 $outputpath/${videofile%_1_BTC.mp4}.mp4
elif [ "find ${videofile%_1_BTC.mp4}* | wc -l" == 3 ]; then
echo "DUO GEN"
timecode=$( ffmpeg -i "$videofile" 2>&1 | awk '$1 ~ /^timecode/ {print $NF}' | uniq )
ffmpeg -i ${videofile%_1_BTC.mp4}_2.mxf -i $videofile -filter_complex "hstack" -ac 2 -flags global_header -timecode $timecode -c:v libx264 $outputpath/${videofile%_1_BTC.mp4}.mp4
else
echo "Error: incorrect number of files"
fi
fi
done
yes no | nice -n 15 rm -f /var/run/wrangling/quadGen.pid
fi
fi
这部分:
echo find "${videofile%_1_BTC.mp4}"* | wc -l
当我得到一个值时,这可以正常工作,我无法弄清楚如何正确比较它
解决方案
双引号引入一个字符串,而不是一个要运行的命令。不要使用它们。
改用命令替换:
if [ $(find ${videofile%_1_BTC.mp4}* | wc -l) == 5 ]; then
您可以同时使用==
和-eq
:==
比较字符串,-eq
比较整数。
推荐阅读
- windows - PowerShell:在特定节点上启动特定任务/进程
- ios - Flutter App在TestFlight中崩溃但在模拟器上工作
- django - 如何在 Django 模板中更新由“with”设置的变量
- git - 我以交互方式在 master 分支上重新建立基础,因此现在功能分支看起来比它早早开始了。我怎样才能解决这个问题?
- java - 我的 android 应用程序在共享 gif 时崩溃
- figma - Figma 有 Angular Material UI Kit 吗?
- sql-server - 重新链接表后 MSAccess 缺少列
- c# - 如何检索有关此批量加载错误的更多信息?
- r - lpSolve 长时间运行而未完成
- arrays - 如何对分配给变量的jsob数组进行排序?