shell 中 apt-get 的日志输出

shell 中 apt-get 的日志输出

apt-get在我的 go 程序中使用 via exec 。像这样:

func aptInstall(pkg, ver string) {
    args := []string{"install", "-y", fmt.Sprintf("%s=%s", pkg, ver)}
    cmd := exec.Command("apt-get", args...)
    cmd.Env = append(os.Environ(), "LANG=en")
    out, err := cmd.CombinedOutput()
    log("apt install", "cmd", fmt.Sprintf("apt-get %s",
        strings.Join(args, " ")), "out", string(out), "err", err)
}

记录的消息包含:

    Selecting previously unselected package nnrforms.
    (Reading database ... 
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 219690 files and directories currently installed.)
    Preparing to unpack .../nnrforms_531-82a94c0_amd64.deb ...
    Unpacking nnrforms (531-82a94c0) ...
    Setting up nnrforms (531-82a94c0) ...
    [0;1;31mFailed to reload nginx.service: Unit nginx.service not found.[0m

我怎样才能知道apt-get它没有在交互模式下运行,并抑制进度消息,例如Reading database ...不添加 ANSI-ESC 着色?

更新

尝试了两个答案,着色被 set 删除TERM=dumb,但进度信息仍然存在:

- 20230518_144401.646+0800: apt install
  cmd: apt-get install -qqqy -o Dpkg::Progress-Fancy="0" -o APT::Color="0" -o Dpkg::Use-Pty="0" a2up=72-5683a16
  err: <nil>
  out: 
- 20230518_144414.275+0800: apt remove
  cmd: apt-get remove -qqqy -o Dpkg::Progress-Fancy="0" -o APT::Color="0" -o Dpkg::Use-Pty="0" a2s
  err: exit status 100
  out: |
    (Reading database ... 
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 219699 files and directories currently installed.)
    Removing a2up (72-5683a16) ...
    Failed to disable unit: Unit file a2up.service does not exist.
    dpkg: error processing package a2up (--remove):
     installed a2up package post-removal script subprocess returned error exit status 1
    dpkg: too many errors, stopping
    Errors were encountered while processing:
     a2up
    Processing was halted because there were too many errors.
    E: Sub-process /usr/bin/dpkg returned an error code (1)

请注意,-qqqy抑制了大多数输出​​消息apt-get install,但似乎不适用于apt-get remove

答案1

-q您可以使用、-qq和来控制输出的详细程度-qqq以抑制警告2>/dev/null

sudo apt install neofetch -yq -o Dpkg::Progress-Fancy="0" -o APT::Color="0" -o Dpkg::Use-Pty="0" 2> /dev/null

-o Dpkg::Progress-Fancy="0"取消进度条

-o APT::Color="0"禁用颜色的使用

-o Dpkg::Use-Pty="0"隐藏Reading database消息

我跑

sudo apt install neofetch -yq -o Dpkg::Progress-Fancy="0" -o APT::Color="0" -o Dpkg::Use-Pty="0" 2> /dev/null > out

在此输入图像描述

希望这可以帮助

相关内容