Bash脚本错误

时间:2012-06-28 18:48:05

标签: bash mv

嘿伙计我在我的第二个嵌套的其他地方遇到错误,我认为它与将文件移动到名称中有空格的文件夹有关,但我不确定,所以我尝试添加另一个嵌套,如果这将允许我重命名,它也抛出一个错误虽然格式化?

我认为真正的问题是如果我错了?

错误

/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

1 个答案:

答案 0 :(得分: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