我正在尝试制作我的第一个“真正”bash脚本来完成一些实际的工作,而这些工作既可以手动执行,也可以通过cron作业执行。
脚本应该从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检查,但是它失败了(从比较bash脚本中的md5和获得了md5sum检查部分)。
如果我删除了md5sum部分,那么脚本就可以工作了,但是现在脚本在用这段代码时发生了问题。
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 access。
更新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但仍有一些问题:
更新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_dir的写访问权,以及是否有提示请求新位置或提高用户权限的dest_dir or再一次,我为到目前为止所得到的帮助感到高兴:)
发布于 2017-02-28 10:15:10
你的所作所为让你迷失了方向。把你的剧本和这个比较一下。
#!/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回答你的评论问题。
tar -xf等等?
我知道的唯一现成的解决方案是在调试模式下运行Bash,如果这是您想要的话。您可以在脚本中的任何地方打开它
设置-x
然后你关掉它,用:
集+x
您也可以在命令行中这样做。
您还可以将整个脚本设置为在该模式下运行。
#!/bin/bash -x
或者您可以告诉解释器从命令行在此模式下运行,
bash -x ~/bin/Yourscript.bashhttps://stackoverflow.com/questions/42497566
复制相似问题