用于安装/更新ffmpeg静态构建的bash脚本

时间:2017-02-27 23:18:50

标签: linux bash

我试图让我的第一个"真实" bash脚本做一些真正的工作,而不是手动和通过cron作业执行。

该脚本应从https://johnvansickle.com/ffmpeg

下载并安装ffmpeg-static-build

这是我到目前为止所得到的:

#!/bin/bash
# Still to come, see if the script runs with root privileges else exit
#Download FFMPEG static daily build and it's md5
cd ~
wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz | tar -xf
#wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5
curl -L https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5 | tar -xf | tee -a https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz | cut -d\  -f1 | md5sum > md5sum.txt


#Chech the md5 is currect
#md5sum -c MD5SUMS
file1="md5sum.txt"
file2="ffmpeg-git-64bit-static.tar.xz.md5"

if ! cmp --silent "$file1" "$file2"; then
    echo "md5sum doesn't match...\n exit" >&2
    exit 1
fi


#tar -xf ffmpeg-*.tar.xz
cp ffmpeg-*-static/ff* /usr/bin/
cp ffmpeg-*-static/ff* /usr/local/bin/
cp ffmpeg-*-static/qt-faststart /usr/bin/
cp ffmpeg-*-static/qt-faststart /usr/local/bin/
# Consider change second location to use ln -s
# Remove downloads and do some clean up

rm -fr ffmpeg-*

#EOF

正如您在脚本中所看到的,我想添加md5sum检查但它失败了(从Compare md5 sums in bash script获得了md5sum检查部分)

如果我删除了md5sum部分,脚本正在运行,但是现在脚本在|中失败了tar -xf |部分使用此代码

2017-02-28 00:10:24 (617 KB/s) - ‘ffmpeg-git-64bit-static.tar.xz’ saved [17564756/17564756]

tar: option requires an argument -- 'f'
Try 'tar --help' or 'tar --usage' for more information.
tee: 'https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz': No such file or directory
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    65  100    65    0     0     87      0 --:--:-- --:--:-- --:--:--    87
(23) Failed writing body
md5sum doesn't match...
 exit

由于这是我的第一次,我会感激任何"我4岁"建议和原因

更新

根据此帖子中的建议对脚本进行了一些更改,但我的问题是我没有输出这种状态下的任何内容:(所以我认为现在是时候要求下一个线索了< / p>

#!/bin/bash

# version 0.3z

## Download FFMPEG static daily build and it's md5
## 
## To make this script working you might need to change the below values
## based on whether you are using a 32bit or 64 bit OS
## to obtain the right links you have to have a look @ https://johnvansickle.com/ffmpeg/
## and then change those below
## 
## If you are running on a shared server or dowsn't have root priviliges you might need to uncomment
## point 5.


# a "~" means running users home folder :) and should be different from destination dir
download_dir=~

# as this can change if the ffmpeg is to be stored on ex. shared server
dest_dir=/usr/bin/

# The version it equal the filename from above link
version=ffmpeg-git-64bit-static.tar.xz

## Do not change anything below here!!
source_url=https://johnvansickle.com/ffmpeg/builds/${version}
md5_url=https://johnvansickle.com/ffmpeg/builds/${version}.md5

# Still to come, see if the script runs with write privileges else exit

# 1. Can we enter download directory else exit
cd ${download_dir} ||

        printf "%s\n" "You can't enter this folder.\nPlease chage download_dir in this script..." >&2
        exit 1

# 2. Check the md5 is correct or have changed from last check
## instead of downloading ffmpeg-git-64bit-static.tar.xz every time,
## regardless if it is new or not, I recommend only downloading it
## if the md5 does not match. Would save John some bandwidth at least
## thx for the idea to @LordNeckbeard



## So somehow i'll guess some sed or grep only first part is nessesary :(
## This is getting more advance than expected for a first time script :/

if ! diff <(md5sum ${version}) <(curl -s ${md5_url})

    then
        printf "%s\n" "md5sum doesn't match...\n
                        Downloading new version" >&2
        rm -f ${version} >&2
        curl ${source_url} -o ${download_dir}/${version} >&2
        #exit 2

    elif
        diff <(md5sum ${version}) <(curl -s ${md5_url})
        printf "%s\n" "Nothing new to download" >&2
      exit 3
fi

# 3. untar
tar -xf ffmpeg-git-*-static.tar.xz

# 4. Move builds to destination directories

mv ${download_dir}/ffmpeg-*-static/ff* ${dest_dir}/
mv ${download_dir}/ffmpeg-*-static/qt-faststart ${dest_dir}/

# 5. Make soft links to static builds
ln -sfn ${dest_dir}/qt-faststart /usr/local/bin/qt-faststart
ln -sfn ${dest_dir}/ffmpeg /usr/local/bin/ffmpeg
ln -sfn ${dest_dir}/ffmpeg-10bit /usr/local/bin/ffmpeg-10bit
ln -sfn ${dest_dir}/ffprobe /usr/local/bin/ffprobe
ln -sfn ${dest_dir}/ffserver /usr/local/bin/ffserver

# Remove unzipped folder to do some clean up

rm -fr ffmpeg-git-*-static/

#EOF

注意:请更深入地回答为什么不从源代码编译: 1.尽管有发行版和版本,但预编译的所有Linux版本都可以使用 2.它可以在具有ssh访问权限的共享主机服务器上使用

更新2

#!/bin/bash

# version 0.4z

## Download FFMPEG static daily build and it's md5
## 
## To make this script working you might need to change the below values
## based on whether you are using a 32bit or 64 bit OS
## to obtain the right links you have to have a look @ https://johnvansickle.com/ffmpeg/
## and then change those below
## 
## Finished and working script should be distributed under GPLv3: free as in freedom
## 
## If you are running on a shared server or dowsn't have root priviliges you might need to uncomment
## point 5.


# a "~" means running users home folder :) and should be different from destination dir
download_dir=~

# as this can change if the ffmpeg is to be stored on ex. shared server
dest_dir=/usr/bin/

# The version it equal the filename from above link
version=ffmpeg-git-64bit-static.tar.xz

## Do not change anything below here!!
source_url=https://johnvansickle.com/ffmpeg/builds/${version}
md5="curl -s https://johnvansickle.com/ffmpeg/builds/${version}.md5"

# Still to come, see if the script runs with write privileges else exit

# 1. Can we enter download directory else exit
cd ${download_dir} || 
        printf "%s\n" "You can't enter this folder.\nPlease chage download_dir in this script..." >&2
        exit 1

# 2. Check the md5 is correct or have changed from last check
## instead of downloading ffmpeg-git-64bit-static.tar.xz every time,
## regardless if it is new or not, I recommend only downloading it
## if the md5 does not match. Would save John some bandwidth at least
## thx for the idea to @LordNeckbeard

## This is getting more advance than expected for a first time script :/

if diff <(md5sum ${version}) <(${md5})

    then
        printf "%s\n" "No new version availeble" >&2
        exit 1

elif ! diff <(md5sum ${version}) <(${md5})
    then
        rm -f ${version}
        curl ${source_url} > ${version}
        exit 0

        #only proceed if downloaded version match it's md5
        if ! diff <(md5sum ${version}) <(${md5})
            then
            rm -f ${version}
            printf "%s\n" "Downloaded version is damaged, try later\ndamaged version have been deleted" >&2
            exit 1
        fi

            # 3. untar
            tar -xf ffmpeg-git-*-static.tar.xz

            # 4. Move builds to destination directories
            mv ${download_dir}/ffmpeg-*-static/ff* ${dest_dir}/
            mv ${download_dir}/ffmpeg-*-static/qt-faststart ${dest_dir}/

            # 5. Make soft links to static builds
            ln -sfn ${dest_dir}/qt-faststart /usr/local/bin/qt-faststart
            ln -sfn ${dest_dir}/ffmpeg /usr/local/bin/ffmpeg
            ln -sfn ${dest_dir}/ffmpeg-10bit /usr/local/bin/ffmpeg-10bit
            ln -sfn ${dest_dir}/ffprobe /usr/local/bin/ffprobe
            ln -sfn ${dest_dir}/ffserver /usr/local/bin/ffserver

            # Remove unzipped folder to do some clean up
            rm -fr ffmpeg-git-*-static/
            printf "%s\n" "Going to install new version" >&2
            exit 1
fi
#EOF

但仍有一些问题:(

  1. 运行此脚本会返回:一个blanc shell,但我预计会有一个printf语句
  2. 当我试图缩小问题范围并回到基本状态并尝试仅使用&#34时运行脚本时,如果&#34;部分失败44:语法错误:&#34;(&#34;意外
  3. 运行相同的&#34;如果&#34;部分直接输入shell /终端本身就很开心!!

    if diff <(md5sum ffmpeg-git-64bit-static.tar.xz) <(curl -s "https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5"); then printf "%s\n" "No new version availeble" >&2; elif ! diff <(md5sum ffmpeg-git-64bit-static.tar.xz) <(curl -s "https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5"); then rm -f ffmpeg-git-64bit-static.tar.xz; curl https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz > ffmpeg-git-64bit-static.tar.xz; printf "%s\n" "Going to install new version" >&2; fi
    
  4. 我很困惑

  5. 更新3 @tomas也引起了我的注意,问题3是sh update_ffmpeg.sh运行脚本的错误,它应该只是通过输入脚本ex的完整路径从它的位置执行。 /home/username/update_ffmpeg.sh

    分享当前的工作版(但仍然不完整):

    #!/bin/bash
    
    # version 0.4z
    
    ## Download FFMPEG static daily build and it's md5
    ## 
    ## To make this script working you might need to change the below values
    ## based on whether you are using a 32bit or 64 bit OS
    ## to obtain the right links you have to have a look @ https://johnvansickle.com/ffmpeg/
    ## and then change those below
    ## 
    ## Finished and working script should be distributed under GPLv3: free as in freedom
    ## 
    ## If you are running on a shared server or dowsn't have root priviliges you might need to uncomment
    ## point 5.
    
    
    # a "~" means running users home folder :) and should be different from destination dir
    download_dir=~
    
    # as this can change if the ffmpeg is to be stored on ex. shared server
    dest_dir=/usr/bin/
    
    # The version it equal the filename from above link
    version=ffmpeg-git-64bit-static.tar.xz
    
    ## Do not change anything below here!!
    source_url=https://johnvansickle.com/ffmpeg/builds/${version}
    md5_url=https://johnvansickle.com/ffmpeg/builds/${version}.md5
    
    # Still to come, see if the script runs with write privileges else exit
    
    # 1. Can we enter download directory else exit
    cd ${download_dir} || {
        printf "%s\n" "You can't enter this folder." "Please change download_dir in this script..." >&2
        exit 1
    }
    
    # 2. Check the md5 is correct or have changed from last check
    ## instead of downloading ffmpeg-git-64bit-static.tar.xz every time,
    ## regardless if it is new or not, I recommend only downloading it
    ## if the md5 does not match. Would save John some bandwidth at least
    ## thx for the idea to @LordNeckbeard
    
    ## This is getting more advance than expected for a first time script :/
    
    if ! diff <(md5sum ${version}) <(curl -s ${md5_url})
    then
        # Sum doesn't match is not really an error... I comment out the redirection.
        printf "%s\n" "md5sum doesn't match..." "Downloading new version"
        rm -f ${version}
        curl ${source_url} -o ${download_dir}/${version}
    
            # 3. untar
            tar -xf ffmpeg-git-*-static.tar.xz
    
            # 4. Move builds to destination directories
            mv ${download_dir}/ffmpeg-*-static/ff* ${dest_dir}/
            mv ${download_dir}/ffmpeg-*-static/qt-faststart ${dest_dir}/
    
            # 5. Make soft links to static builds
            ln -sfn ${dest_dir}/qt-faststart /usr/local/bin/qt-faststart >&2
            ln -sfn ${dest_dir}/ffmpeg /usr/local/bin/ffmpeg >&2
            ln -sfn ${dest_dir}/ffmpeg-10bit /usr/local/bin/ffmpeg-10bit >&2
            ln -sfn ${dest_dir}/ffprobe /usr/local/bin/ffprobe >&2
            ln -sfn ${dest_dir}/ffserver /usr/local/bin/ffserver >&2
    
            # Remove unzipped folder to do some clean up
            rm -fr ffmpeg-git-*-static/
            printf "%s\n" "Installing new version" >&2
    else
        printf "%s\n" "Nothing new to download" # >&2 -- Why is this an error?
        exit # 3 -- I don't think this is any error. You checked and it's fine.
    
    fi
    
    #EOF
    

    下一个任务:

    • 让脚本检查当前用户是否具有写访问权限 download_dirdest_dir其他人退出并提示要求新的 位置或提升用户权利

    再一次,我很高兴能够获得迄今为止我得到的所有帮助:)

1 个答案:

答案 0 :(得分:3)

你迷失了你正在做的事情。将您的脚本与此脚本进行比较。

#!/bin/bash
# Still to come, see if the script runs with root privileges else exit

# Download FFMPEG static daily build and it's md5

# or exit if not
cd ~ || exit 2

# 1. get the file
wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz

# 2. Check the md5 is correct
if ! diff <(md5sum ffmpeg-git-64bit-static.tar.xz) \
          <(curl -L https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5)
then 
    printf "%s\n" "md5sum doesn't match..." >&2 
    exit 1
fi

# 3. untar
tar -xf ffmpeg-git-64bit-static.tar.xz

# 4. and so on..
cp ffmpeg-*-static/ff* /usr/bin/
cp ffmpeg-*-static/ff* /usr/local/bin/
cp ffmpeg-*-static/qt-faststart /usr/bin/
cp ffmpeg-*-static/qt-faststart /usr/local/bin/
# Consider change second location to use ln -s
# Remove downloads and do some clean up

rm -fr ffmpeg-*

#EOF

对您的更新版本进行了一些修复。我删除了你的评论以使我自己可见。

#!/bin/bash
download_dir=~
dest_dir=/usr/bin/
version=ffmpeg-git-64bit-static.tar.xz
source_url=https://johnvansickle.com/ffmpeg/builds/${version}
md5_url=https://johnvansickle.com/ffmpeg/builds/${version}.md5

# You need braces to build blocks.
# As it was, your script terminated at the exit. Regardless. End of story.
cd ${download_dir} || {
    printf "%s\n" "You can't enter this folder." "Please change download_dir in this script..." >&2
    exit 1
}


if ! diff <(md5sum ${version}) <(curl -s ${md5_url})
then
    # Sum doesn't match is not really an error... I comment out the redirection.
    printf "%s\n" "md5sum doesn't match..." "Downloading new version" # >&2
    rm -f ${version} # >&2 -- Why would you redirect any output to stderr?
    curl ${source_url} -o ${download_dir}/${version} # >&2 -- Same as above.
else
    # You've done this already.
    # diff <(md5sum ${version}) <(curl -s ${md5_url})
    printf "%s\n" "Nothing new to download" # >&2 -- Why is this an error?
    exit # 3 -- I don't think this is any error. You checked and it's fine.
    # It might stay if you really MEAN it.
fi

# I'm not checking further.

tar -xf ffmpeg-git-*-static.tar.xz
mv ${download_dir}/ffmpeg-*-static/ff* "${dest_dir}"
mv ${download_dir}/ffmpeg-*-static/qt-faststart "${dest_dir}"

ln -sfn ${dest_dir}/qt-faststart /usr/local/bin/qt-faststart
ln -sfn ${dest_dir}/ffmpeg /usr/local/bin/ffmpeg
ln -sfn ${dest_dir}/ffmpeg-10bit /usr/local/bin/ffmpeg-10bit
ln -sfn ${dest_dir}/ffprobe /usr/local/bin/ffprobe
ln -sfn ${dest_dir}/ffserver /usr/local/bin/ffserver

rm -fr ffmpeg-git-*-static

评论问题的答案。

  1. 什么时候被确定为块?

    Here你可以获得更广泛的块图片。在Bash中,它们被称为列表。运行这个:

    man bash | grep -A 30 'Compound Commands'
    

    看看Bash的男人对他们有什么好的开始。 This guide应该更加平易近人。

  2. 如何将当前运行命令输出到控制台ex。 tar -xf等等?

    我知道的唯一现成解决方案是在调试模式下运行Bash,如果这是你想要的。您可以使用

    在脚本中的任何位置打开它
     set -x
    

    然后用以下方式关闭它:

    set +x
    

    您可以在命令行中执行相同的操作。

    您还可以将整个脚本设置为在shebang中以此模式运行。

    #!/bin/bash -x
    

    或者您可以从命令行告诉解释器以此模式运行,

    bash -x ~/bin/your_script.bash