如何在没有用户交互的情况下安装软件包?

我有一个脚本可以下载并replaceDebian中的内核头文件。

function fixHeaders(){ #Replace the kernel headers from OVH with standard kernel headers... aptitude -y install linux-image-2.6.32-5-amd64 sed s/'GRUB_DEFAULT=0'/'GRUB_DEFAULT=1'/g update-grub echo "Rebooting the machine. Run this script again after reboot and choose option 2." sleep 1 reboot } 

我遇到的问题是,在aptitude下载软件包后,它将脚本引入到文本gui中,并询问用户一堆问题。 有什么办法可以跳过这个或在适当的时间发送标签/进入select“确定”的所有答案?

根据Daniel t的评论,我可以用DEBIAN_FRONTEND=noninteractive来做到这一点

 DEBIAN_FRONTEND=noninteractive /usr/bin/apt-get install -y -q --force-yes linux-image-2.6.32-5-amd64 

请注意,我引用的这个答案不会消除所有对话,它仍然会显示APT / DPKG认为至关重要的内容 。 也许最好是尝试第二个选项+使用readline前端进行debconf并准备答案文件。

从姊妹网站引用:

这应该做你所问的; 之后询问configuration问题:

 $ DEBIAN_PRIORITY=critical $ export DEBIAN_PRIORITY $ apt-get upgrade # Wait a long time. Should be almost entirely noninteractive. $ dpkg-reconfigure --default-priority=medium --unseen-only 

或者,您可以尝试之前询问所有configuration问题:

 $ apt-get clean $ cat >> /etc/apt/apt.conf <<EOF // Pre-configure all packages before // they are installed. DPkg::Pre-Install-Pkgs { "dpkg-preconfigure --apt --priority=low"; }; EOF $ apt-get upgrade 

由于您可以识别“适当的时间”(因为input不会改变),所以您可以使用expect的工具发送任何您想要的内容。