AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1266959
Accepted
TADASUKE
TADASUKE
Asked: 2020-08-14 10:44:26 +0800 CST2020-08-14 10:44:26 +0800 CST 2020-08-14 10:44:26 +0800 CST

Bash Shell 脚本:虽然 apt-get update 失败,但退出状态为 0,使用退出状态编写自动更新脚本

  • 772

我正在尝试编写自动更新和升级脚本。

我想利用命令执行的退出状态,

但即使 apt-get update 命令失败,它也会返回退出状态 0。

所以我的脚本无法达到目的。

问题============

为什么 apt-get update 返回 0,我想在命令失败时获取其他数字而不是 0?

为了使我的脚本达到目的,我该如何修改它?

======================

感谢您的阅读!

这是我脚本的更新部分;

#!/bin/bash -x 

### Variables
count=
command_result=""

### Main

echo "$(LC_TIME=en_US.UTF-8 date)" >> log_update

until [ "$count" = "10" ] || [ "$command_result" = "done" ]; do

    sudo apt-get update

    if [ "$?" = "0" ]; then
        echo "Update succeeds." >> ~/log_update
        command_result="done"
    fi

    count=$((count + 1)) 

done

if [ "$command" != "done" ]; then
    echo "Time Out: Update FAILED! Solve Problem." >> log_update
fi
scripts command-line updates bash apt
  • 1 1 个回答
  • 1178 Views

1 个回答

  • Voted
  1. Best Answer
    White Mars
    2020-08-15T06:20:13+08:002020-08-15T06:20:13+08:00

    要理解为什么apt还是apt-get返回0,即使遇到错误,首先要知道这些命令也是某个程序员开发的程序。因此返回到终端的值也由程序的开发者决定。

    我看到apt不起作用的唯一情况是在sudo
    没有超级用户权限的情况下执行 apt 以下是在没有超级用户权限的情况下执行 apt 的片段:

    mars@HP-Notebook:~/Desktop/Practice/cpp$ apt update
    Reading package lists... Done
    E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
    E: Unable to lock directory /var/lib/apt/lists/
    W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
    W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
    mars@HP-Notebook:~/Desktop/Practice/cpp$ echo $?
    100
    

    如您所见,退出代码不是 0。因此我们可以得出结论,命令或程序没有被执行。
    让我们看另一种情况,当没有互联网连接时:

    mars@HP-Notebook:~/Desktop/Practice/cpp$ sudo apt update
    Err:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
      Could not resolve 'security.ubuntu.com'
    Err:2 http://dl.google.com/linux/chrome/deb stable InRelease                                                   
      Could not resolve 'dl.google.com'
    Err:3 http://in.archive.ubuntu.com/ubuntu bionic InRelease                                                     
      Could not resolve 'in.archive.ubuntu.com'
    Err:4 https://download.sublimetext.com apt/stable/ InRelease                     
      Could not resolve 'download.sublimetext.com'
    Err:5 http://in.archive.ubuntu.com/ubuntu bionic-updates InRelease
      Could not resolve 'in.archive.ubuntu.com'
    Err:6 http://in.archive.ubuntu.com/ubuntu bionic-backports InRelease
      Could not resolve 'in.archive.ubuntu.com'
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    All packages are up to date.
    W: Failed to fetch http://in.archive.ubuntu.com/ubuntu/dists/bionic/InRelease  Could not resolve 'in.archive.ubuntu.com'
    W: Failed to fetch http://in.archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease  Could not resolve 'in.archive.ubuntu.com'
    W: Failed to fetch http://in.archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease  Could not resolve 'in.archive.ubuntu.com'
    W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease  Could not resolve 'security.ubuntu.com'
    W: Failed to fetch https://download.sublimetext.com/apt/stable/InRelease  Could not resolve 'download.sublimetext.com'
    W: Failed to fetch http://dl.google.com/linux/chrome/deb/dists/stable/InRelease  Could not resolve 'dl.google.com'
    W: Some index files failed to download. They have been ignored, or old ones used instead.
    mars@HP-Notebook:~/Desktop/Practice/cpp$ echo $?
    0
    

    如您所见,即使我们遇到错误,退出代码也是 0。原因是程序或命令确实执行成功。但是,它无法更新软件包列表以升级需要升级的软件包。

    对此唯一的解释是,aptcommand 的开发者没有将更新包列表失败视为错误来停止命令本身并返回错误退出状态码。相反,该命令在执行后提供警告。

    为了更好地理解,我将举一个 C 程序的例子:

    #include<stdio.h>
    
    int main(int argc, char *argv[]) {
        if(argc==2) 
            printf("Welcome Master %s\n", argv[1]);
        else {
            fprintf(stderr, "Usage : %s <name>\n", argv[0]);
            return 1;
        }
        return 0;
    }
    

    输出:

    mars@HP-Notebook:~/Desktop/Practice/cpp$ ./batman
    Usage : ./batman <name>
    mars@HP-Notebook:~/Desktop/Practice/cpp$ echo $?
    1
    mars@HP-Notebook:~/Desktop/Practice/cpp$ ./batman Bruce
    Welcome Master Bruce
    mars@HP-Notebook:~/Desktop/Practice/cpp$ echo $?
    0
    

    如您所见,有两种不同的退出状态代码,因为作为一名程序员,我认为不带参数执行的命令是错误的,应该以退出代码“1”终止。(我可以选择任何值)。如果命令成功执行,我返回“0”作为退出状态码,表示没有遇到错误。

    让我们再举一个例子:

    #include<stdio.h>
    
    int main(int argc, char *argv[]) {
        if(argc==2) 
            printf("Welcome Master %s\n", argv[1]);
        else
            printf("ERROR!!\nUsage : %s <name>\n", argv[0]);
    
        return 0;
    }
    

    输出:

    mars@HP-Notebook:~/Desktop/Practice/cpp$ ./batman_error 
    ERROR!!
    Usage : ./batman_error <name>
    mars@HP-Notebook:~/Desktop/Practice/cpp$ echo $?
    0
    mars@HP-Notebook:~/Desktop/Practice/cpp$ ./batman_error Bruce
    Welcome Master Bruce
    mars@HP-Notebook:~/Desktop/Practice/cpp$ echo $?
    0
    

    同样的程序,但这次程序员(即我)没有考虑用不同的退出状态代码终止程序。因此,即使终端打印错误输出,退出状态代码也是“0”。

    结论

    根据情况返回哪个值(即退出状态码)取决于程序的开发人员。
    我希望这可以清除退出状态代码的概念。

    对您的 bash 脚本的建议

    我看到你正在使用一个循环,apt update如果它没有成功执行,它将尝试执行 10 次命令。老实说,如果它第一次不起作用,那么接下来的 9 次也不会起作用。因此,创建一个循环是毫无意义的。

    现在,如果您想检查错误,请使用嵌套的 if-else 条件。您可以检查第一级的状态代码并检查apt第二级执行的错误(即退出代码“0”),您可以使用以下内容:

    sudo apt update | grep "Err"
    

    如果grep能够获取一行,则将其作为错误存储在您的日志文件中,否则更新成功。

    • 1

相关问题

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

  • 如何让 Ubuntu 减少检查更新的频率?对于 11.04 及更高版本对于赶时间的人!通过 Ubuntu 软件中心

  • 如何启用自动更新?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve