2012-06-28 218 views
0

嘿傢伙我在我的第二個嵌套的else附近出現錯誤,我認爲它與將文件移動到名稱中帶有空格的文件夾有關,但我不確定,所以我嘗試添加另一個嵌套,如果這將允許我重命名,它也拋出一個錯誤,雖然關於格式?Bash腳本錯誤

我認爲真正的問題是我嵌套如果錯了?

錯誤

/bin/y2m: line 16: syntax error near unexpected token `then' 
/bin/y2m: line 16: `  if[ -z "$newname" ]; then' 

代碼

#!/bin/bash 

address=$1 
newname=$2 
regex='v=(.*)' 
if [[ $address =~ $regex ]]; then 
    video_id=${BASH_REMATCH[1]} 
    video_id=$(echo $video_id | cut -d'&' -f1) 

    if[ -z "$newname" ]; then 
     video_title="$newname" 
    else 
     video_title="$(youtube-dl --get-title $address)" 
    fi 

    youtube-dl -o "$video_title".flv $address 
    ffmpeg -i "$video_title".flv -acodec libmp3lame -ac 2 -ab 256k -vn -y "$video_title".mp3 

     if [ -d "/media/SDHC CARD/Music/y2m" ]; then 
      mv "$video_title".mp3 "/media/SDHC CARD/Music/y2m" 
      echo "Moving to Phone Card" 
     else 
      mv "$video_title".mp3 ~/Music 
      echo "Moving to Music Folder" 
     fi 

    rm "$video_title".flv 
else 
    echo "Sorry but you seemed to broken the interwebs." 
fi 

回答

2

我忘了我的空間,它if[應該if [

我修改一下這個

#!/bin/bash 
    # Youtube to MP3 Bash Script 

    # CPR : Jd Daniel :: Ehime-ken 
    # REQ : sudo apt-get install youtube-dl && youtube-dl -U 
    # REQ : sudo apt-get install lame 
    # REQ : ffmpeg [use: http://ubuntuforums.org/showpost.php?p=4907079&postcount=1] 

    address=$1 
    newname=$2 
    regex='v=(.*)' 

    if [[ $address =~ $regex ]]; then 
      video_id=${BASH_REMATCH[1]} 
      video_id=$(echo $video_id | cut -d'&' -f1) 

      if [ -n "$newname" ]; then 
        video_title="$newname" 
      else 
        video_title="$(youtube-dl --get-title $address)" 
      fi 

      youtube-dl -o "$video_title".flv $address 
      ffmpeg -i "$video_title".flv -acodec libmp3lame -ac 2 -ab 256k -vn -y "$video_title".mp3 

        if [ -d "/media/SDHC CARD/Music/y2m" ]; then 
          cp "$video_title".mp3 ~/Music && mv "$video_title".mp3 "/media/SDHC CARD/Music/y2m" 
          echo "Saving to Phone" 
        else 
          mv "$video_title".mp3 ~/Music 
          echo "Saving to Local" 
        fi 

      rm "$video_title".flv 
    else 
      echo "Sorry but you seemed to broken the interwebs." 
    fi