首页 > 解决方案 > 将文件数与整数 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

当我得到一个值时,这可以正常工作,我无法弄清楚如何正确比较它

标签: bash

解决方案


双引号引入一个字符串,而不是一个要运行的命令。不要使用它们。

改用命令替换:

if [ $(find ${videofile%_1_BTC.mp4}* | wc -l) == 5 ]; then

您可以同时使用==-eq==比较字符串,-eq比较整数。


推荐阅读