2014-05-18 43 views
0

我想創建一個腳本,它將使用4個id標籤之一來搜索驅動器上的MP3文件。到目前爲止,我設法創造了這樣的東西,但它根本不起作用。有人可以建議我解決什麼問題嗎?使用id3搜索MP3的腳本

#!/bin/bash  

while getopts ":atbg:" opt; do 
case $opt in 
a) artist=${OPTARG} 
;; 
b) album=${OPTARG} 
;; 
t) title=${OPTARG} 
;; 
g) genre=${OPTARG} 
;; 
esac 
done 
find . -name '*.mp3' -print0 | while read -d $'\0' file 
do 
    checkere=0 
    if [ "$album" != NULL ] 
    then 
     if [ !($(id3info "$file" | grep '$artist' sed -e 's/.*: //g')) ] 
     then 
      $checkere=1 
     fi 
    fi 
    if [ "$title" != NULL ] 
    then 
     if [ !($(id3info "$file" | grep '$title' sed -e 's/.*: //g')) ] 
     then 
      $checkere=1 
     fi 
    fi 
    if [ "$album" != NULL ] 
    then 
     if !($(id3info "$file" | grep '$album' sed -e 's/.*: //g')) 
     then 
      $checkere=1 
     fi 
    fi 
    if [ "$genre" != NULL ] 
    then 
     if !($(id3info "$file" | grep '$genre' sed -e 's/.*: //g')) 
     then 
      $checkere=1 
     fi 
    fi 
    if [ $checkere -eq 0 ] 
    then 
     echo $file   
    fi 
done 

回答

0
#!/bin/bash 
# Process command line args 
while getopts a:b:t:g: arg ; do case $arg in 
    a) artist=${OPTARG} ;; 
    b) album=${OPTARG} ;; 
    t) title=${OPTARG} ;; 
    g) genre=${OPTARG} ;; 
    :) echo "${0##*/}: Must supply an argument to $OPTARG." ; exit 1 ;; 
    \?) echo "Invalid option. Abort" ; exit 1 ;; 
    esac 
    done 
shift $(($OPTIND - 1)) 
[ "$#" -eq 0 ] || { echo "Incorrect usage" ; exit 1 ; } 

# Find matching files 
find . -name '*.mp3' -print0 | 
while read -r -d $'\0' file 
do 
    info=$(id3info $file) 
    [ "$artist" ] && { echo "$info" | grep -q "=== TPE1 (Lead performer(s)/Soloist(s)): $artist$" || continue ; } 
    [ "$album" ] && { echo "$info" | grep -q "=== TALB (Album/Movie/Show title): $album$" || continue ; } 
    [ "$title" ] && { echo "$info" | grep -q "=== TIT2 (Title/songname/content description): $title$" || continue ; } 
    [ "$genre" ] && { echo "$info" | grep -q "=== TCON (Content type): $genre$" || continue ; } 
    echo "$file" 
done 

使用範例:

mp3search -a "The Rolling Stones" -t "Let It Bleed"