為什麼“eval”在此程式碼中不起作用?

為什麼“eval”在此程式碼中不起作用?

我編寫了以下程式碼,旨在建立一個包含低解析度媒體檔案清單的檔案:

#!/usr/bin/bash

find "$PWD" -type f -iname "*.avi" -execdir ~/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh {} + >~/pCloudDrive/VisualArts/lowres.films
find "$PWD" -type f -iname "*.mkv" -execdir ~/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh {} + >>~/pCloudDrive/VisualArts/lowres.films
find "$PWD" -type f -iname "*.mp4" -execdir ~/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh {} + >>~/pCloudDrive/VisualArts/lowres.films

正如您所看到的,上面的程式碼printer.sh呼叫又執行以下程式碼:

#!/usr/bin/bash

#The script is meant to print only the results of low resolution, that is starting with 1, 2, 3, 4, 5

if [[ $(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $1) == 2* ]]; then
  echo "$(realpath $1)" && ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $1 
elif [[ $(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $1) == 3* ]]; then
  echo "$(realpath $1)" && ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $1 
elif [[ $(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $1) == 4* ]]; then
  echo "$(realpath $1)" && ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $1 
elif [[ $(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $1) == 5* ]]; then
  echo "$(realpath $1)" && ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $1 

fi

想要用我修改的變數取代程式碼中的重複printer.sh

#!/usr/bin/bash

output=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $1)
if [[ $($output) == 2* ]]; then
  echo "$(realpath $1)" && eval "$output"
elif [[ $($output) == 3* ]]; then
  echo "$(realpath $1)" && eval "$output"
elif [[ $($output) == 4* ]]; then
  echo "$(realpath $1)" && eval "$output"
elif [[ $($output) == 5* ]]; then
  echo "$(realpath $1)" && eval "$output" 
fi

現在它不起作用,我得到的輸出如下所示:

/home/jerzy/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh: line 6: 1920x1024: command not found
/home/jerzy/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh: line 8: 1920x1024: command not found
/home/jerzy/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh: line 10: 1920x1024: command not found
/home/jerzy/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh: line 12: 1920x1024: command not found
/home/jerzy/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh: line 6: 1920x800: command not found
/home/jerzy/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh: line 8: 1920x800: command not found
/home/jerzy/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh: line 10: 1920x800: command not found
/home/jerzy/CS/SoftwareDevelopment/MySoftware/Bash/lowresolution_finder/printer.sh: line 12: 1920x800: command not found

我做錯了什麼?我該如何重寫它?

答案1

看起來你想要的是這樣的:

output=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$1")
if [[ $output == [2345]* ]]
then 
  echo "$(realpath "$1")" "$output"
fi

eval根本不知道你為什麼要用這裡。eval用於執行包含 shell 程式碼的字串。的輸出ffprobe不是程式碼。

相關內容