如何使 APT 在 Bash 脚本中对所有安装假设为是并强制为是

问题描述 投票:0回答:5

我目前正在使用 Linux 并想编写一个 Bash 脚本来按照我想要的方式设置一台新机器。

为了做到这一点,我想在上面安装不同的东西,等等。

我在这里想要实现的是在 Bash 脚本的顶部进行设置,这将使 APT 接受脚本执行期间提出的所有 [y/n] 问题。

我要自动接受的问题示例:

 After this operation, 1092 kB of additional disk space will be used. Do you want to continue? [Y/n]

我刚开始创建文件,所以这是我目前所拥有的:

#!/bin/bash

# Constants

# Set APT to accept all [y/n] questions
>> some setting here <<

# Update and upgrade APT
apt update;
apt full-upgrade;

# Install terminator
apt install terminator
bash apt apt-get
5个回答
11
投票

apt
旨在交互使用。如果你想自动化,看看
apt-get
,特别是它的
-y
选项:

-y, --yes, --assume-yes

自动提示是;假设“是”作为对所有提示的回答并以非交互方式运行。如果一个不受欢迎的 情况,例如更改保留的包,尝试安装 然后发生未经身份验证的包或删除基本包 apt-get 将中止。配置项:APT::Get::Assume-Yes.

另见

man apt-get
更多选项。


3
投票

如果你确实想像你说的那样在文件的顶部设置一次然后忘记它,你可以使用

APT_CONFIG
环境变量。见
apt.conf

echo "APT::Get::Assume-Yes=yes" > /tmp/_tmp_apt.conf
export APT_CONFIG=/tmp/_tmp_apt.conf

apt-get update
apt-get install terminator
...

3
投票

apt

apt -o Apt::Get::Assume-Yes=true install <package>

参见:

man apt
man apt.conf


1
投票

您可以设置 APT 以永久假设“是”,如下所示:

echo "APT::Get::Assume-Yes \"true\";\nAPT::Get::allow \"true\";" | sudo tee -a  /etc/apt/apt.conf.d/90_no_prompt

0
投票

另一种将它设置在脚本顶部的简单方法是使用命令

alias apt-get="apt-get --assume-yes"
,这会导致所有后续调用
apt-get
以包含 --assume-yes 参数。例如,
apt-get upgrade
会自动通过 bash 转换为 apt-get
--assume-yes upgrade"

请注意,这可能会导致错误,因为一些 apt-get 子命令不接受 --assume-yes 参数。例如,

apt-get help
将被转换为
apt-get --assume-yes help
并返回错误,因为 help 子命令不能与 --assume-yes 一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.