Variable
為什麼 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
我做錯了什麼?我應該如何重寫它?
看起來你想要的是這樣的:
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
不是程式碼。