我有一个运行命令apt-get update的脚本,但是由于没有互联网,脚本无法检测到失败。
也就是说,如果apt-get不是以根权限运行的,那么它将使用code 100退出。
所以我可以做个检查
if apt-get update
then
echo "success!"
else
echo "something went wrong
fi但是,如果apt-get update在没有互联网连接的情况下运行,它会产生错误消息,一些是STDOUT,另一些是STDERR。
这是给STDERR的
..。 W:找不到http://archive.ubuntu.com/ubuntu/dists/trusty/InRelease W:找不到http://security.ubuntu.com/ubuntu/dists/trusty-security/InRelease .
这个交给STDOUT
...
Err http://archive.ubuntu.com trusty-updates Release.gpg
Temporary failure resolving 'archive.ubuntu.com'
Ign https://deb.nodesource.com trusty InRelease
Ign https://deb.nodesource.com trusty Release.gpg
...但最终它仍然返回0的退出。
如何检测由于没有互联网访问而导致的apt-get upgrade故障?
发布于 2016-03-05 06:48:10
我不确定这是不是傻瓜,但我想你可以测试互联网连接是否工作,但我不确定是否一个错误是由没有互联网连接。
cd [directory]
wget [just download any file aslong as you know the name.]
[ -f /[currentDir]/[filename] ] && wall "Internet Connection Is Existant" || wall "Any errors are likely caused by internet issues."
rm [file name just so it doesn't mess things up later.]你可以把“墙”改成“回声”--我就像烦我的朋友一样,因为我经常用SSH。(1/2 debian和1/2次在SSH.在终端应用程序或外壳上。)
发布于 2016-03-05 13:08:04
这里有一个更好的存根:
if apt-get update 2> /tmp/apt-get-errors
then
if grep -q "^W: Failed to fetch" /tmp/apt-get-errors
then
echo "success!"
else
echo apt-get failed to fetch
fi
else
echo "something went wrong
fi
rm /tmp/apt-get-errors其基本思想是将apt的stderr输出捕获到一个文件中,然后搜索该文件中已知的坏模式,然后宣布成功。
它可以通过使用mktemp和可能更好的grep模式来改进,但我现在没有方法测试apt-get。
发布于 2023-04-08 21:18:54
我认为--error-on=any选项实现了所需的功能,尽管到目前为止我在实践中使用它的经验很少。我认为这是apt和apt-get的一个相对较新的特性:
-eany,
apt update -eany
apt update --error-on=any
apt-get update -eany
apt-get update --error-on=any当我关闭笔记本的wifi并运行apt update --error-on=any; echo $?时,echo就会打印100。
https://unix.stackexchange.com/questions/267751
复制相似问题