2017-02-27 45 views
2

我試圖讓我的第一個「真正的」bash腳本來做一些真正的工作,可以手動執行和通過cron作業。bash腳本安裝/更新ffmpeg靜態構建

腳本應該從https://johnvansickle.com/ffmpeg

這裏下載並安裝ffmpeg的靜電建造就是我這麼遠:

#!/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歲的」建議和爲什麼


更新

已經取得了一些變化到基於這個線程的建議腳本,但我的問題是我沒有輸出什麼這樣在這種狀態:(所以我認爲是時候要求下一個線索

#!/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. 運行此腳本返回:一個相思的外殼,但我預計printf語句的一個
  2. 當我試圖縮小問題以及回到基礎,並試圖只與運行腳本「if」部分失敗,出現44:語法錯誤:「(」意外
  3. 運行直接輸入到shell /終端本身的「if」部分。

    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. 我很困惑

更新3 它已經到了我的注意,還受到質疑3運行腳本由sh update_ffmpeg.sh ofcurse是錯誤的,應該只是@tomas通過輸入腳本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寫訪問否則退出並提示要求新的位置或提升用戶權利

再次,我很高興迄今爲止收到的所有幫助:)

+0

如何經常會被你執行這個的cronjob? – LordNeckbeard

+0

hi @LordNeckbeard hi @LordNeckbeard我在想每週一次的想法 – Joakim

+0

如果你對其他想法持開放態度,只是一種替代解決方案,但是在那個頻率下,我會下載源代碼,然後每週更新一次[編譯](http:// trac .ffmpeg.org /#CompilingFFmpeg)。你可以自定義它,並不會依賴別人進行更新。 – LordNeckbeard

回答

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. 如何將當前運行命令輸出到控制檯前。 tar -xf等?

    我知道的唯一就緒解決方案是在調試模式下運行Bash,如果這就是你想要的。你可以在任何地方在腳本

    set -x 
    

    打開它,然後你把它關掉:

    set +x 
    

    你可以做同樣的命令行。

    您還可以設置整個腳本在shebang中以此模式運行。

    #!/bin/bash -x 
    

    或者你可以告訴從命令行在此模式下運行的解釋,

    bash -x ~/bin/your_script.bash 
    
+0

我知道你基本上是按原樣清理了腳本,但不管每次都下載'ffmpeg-git-64bit-static.tar.xz',而不管它是否是新的,我建議只下載它if md5不匹配。至少可以爲John節省一些帶寬。另外,不需要使用'curl' **和**'wget'。只需一個或另一個就足夠了。 – LordNeckbeard

+1

@LordNeckbeard先檢查好主意。但是不使用兩者的意義何在? – tomasz

+3

只是爲了減少所需的依賴關係的數量。例如,你可以使用'curl -O https:// johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz'而不是'wget https://johnvansickle.com/ffmpeg/builds/ ffmpeg-git-64bit-static.tar.xz',現在你只需要'curl'而不是'curl'和'wget'。 – LordNeckbeard