ehime
ehime

Reputation: 8375

Bash Script errors

Hey guys I'm getting an error right around my second nested else, I think it has something to do with moving the file to a folder with whitespace in its name, but I'm not sure, so I tried adding in another nested if that would allow me to rename, it also is throwing an error though about formatting?

I think the real issue is that I'm nesting ifs wrong?

Error

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

Code

#!/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

Upvotes: 0

Views: 256

Answers (1)

ehime
ehime

Reputation: 8375

I forgot to space my if[ it should be if [

I revised it to this

    #!/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

Upvotes: 2

Related Questions